js零碎知识点集锦

来源:互联网 发布:单片机好学吗 编辑:程序博客网 时间:2024/05/22 13:37

1.

if(10||0){alert("rrr");}//rrrconsole.log(10||0);//10console.log(0||undefined);//undefinedconsole.log(undefined||0);//0console.log(0==undefined);//falseconsole.log(null==undefined);//true

2.

alert(Object.prototype.toString.call(/\s*/g));// [object RegExp]

alert(Object.prototype.toString.call(null)=="[object Object]");//false,但是在ie6中会返回true,

alert(Object.prototype.toString.call(null));//[object Null]

3.安全的类型检测

 在以前的chrome中,typeof /;\s*/g会返回‘function’,在低版本的ie中,Object.prototype.toString.call(null)会返回"[object Object]",高版本的都会返回"[object Null]",并且如果存在iframe中的对象,那么通过instanceof来判断某个对象是某个类的实例也会出现问题,安全的类型检测,通过Object.prototype.toString.call(obj),来判断某个对象是否是某个类的实例。

4.将函数的argumengts内置对象转换成数组。

[].slice.call(arguements);

function argTest(){<span style="color:#ff0000;">var convertedArgs=[].slice.call(arguments);</span>var slicedArray=["123","qww"].slice();console.log(slicedArray.join(","));//123,qwwconsole.log(typeof slicedArray);//objectconsole.log(Object.prototype.toString.call(convertedArgs));//[object Array]console.log(Object.prototype.toString.call(arguments));//[object Arguments]}argTest(1,2);

5.hasOwnProperty和isPrototypeOf

obj2.hasOwnProperty(propertyName) 用于判断propertName是否是obj2的实例属性,如果是实例属性就返回TRUE,否则返回FALSE
obj1.isPrototypeOf(obj2) 用于判断obj1是否出现在obj2的原型链中,如果obj2不是对象或者obj1没有出现在obj2的原型中,都会返回false。

6.Date.parse(),Date.UTC();

两者使用起来的结果是一样的,但是如果在new Date()中传入和那两个函数一样的参数,那么只有传入和UTC相同的参数,才会显示北京时间
var parseDate=new Date(Date.parse("2014-07-22T10:21:22"));  <span style="white-space:pre"></span> var UTCDate=new Date(Date.UTC(2014,6,22,10,21,22));  <span style="white-space:pre"></span> var date1=new Date("2014-07-22T10:21:22");  <span style="white-space:pre"></span> var date2=new Date(2014,6,22,10,21,22);  <span style="white-space:pre"></span> console.log(parseDate);//Tue Jul 22 2014 18:21:22 GMT+0800 (中国标准时间)  <span style="white-space:pre"></span> console.log(UTCDate);//Tue Jul 22 2014 18:21:22 GMT+0800 (中国标准时间)  <span style="white-space:pre"></span> console.log(date1);//Tue Jul 22 2014 18:21:22 GMT+0800 (中国标准时间)  <span style="white-space:pre"></span> console.log(date2);//Tue Jul 22 2014 10:21:22 GMT+0800 (中国标准时间)

7.Object.create

该方法用于创建一个对象,传入两个参数,前者是一个prototype对象,后者是一个对象,可以是对象字面量。
var obj=Object.create(Object.prototype,  {     getSize:  {  value:function(){  console.log(this.size);  }},  size:{  value:10  }  }  );   obj.getSize();//10   console.log(obj.hasOwnProperty("getSize"));//true   console.log(obj.hasOwnProperty("toString"));//false







0 0
原创粉丝点击