首页 > Web开发 > 详细

JS中区分参数方法

时间:2016-01-29 12:10:06      阅读:188      评论:0      收藏:0      [点我收藏+]

  

实现功能:在使用cocosjs制作游戏过程中,很多东西都可以重复使用,例如菜单栏等等。今天尝试写了一个自定义的Js文件用作菜单方便以后使用。

 

将菜单按钮,以及触发事件作为参数生成一个层 直接在游戏中使用。

 

因此,就需要在封装时候判断参数类型,以减少不必要的麻烦。

--------

 

 

 

 

 

 

 

 

主要:typeof、instanceof、 constructor、 prototyp

如何判断js中的类型呢,先举几个例子:

var a = "iamstring.";

var b = 222;

var c= [1,2,3];

var d = new Date();

var e = function(){alert(111);};

var f = function(){this.name="22";};

 

最常见的判断方法:typeof

alert(typeof a)   ------------> string

alert(typeof b)   ------------> number

alert(typeof c)   ------------> object

alert(typeof d)   ------------> object

alert(typeof e)   ------------> function

alert(typeof f)   ------------> function

其中typeof返回的类型都是字符串形式,需注意,例如:

alert(typeof a == "string") -------------> true

alert(typeof a == String) ---------------> false

另外typeof 可以判断function的类型;在判断除Object类型的对象时比较方便。

 

判断已知对象类型的方法: instanceof

alert(c instanceof Array) ---------------> true

alert(d instanceof Date) 

alert(f instanceof Function) ------------> true

alert(f instanceof function) ------------> false

 

JS中区分参数方法

原文:http://www.cnblogs.com/z-coder/p/5168395.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!