杂七杂八知识点(易错)

1.查看对象所有属性:

  Object.keys(对象名);

var o = {
  key1: 1,
  key2: 2
};

Object.keys(o);
// ['key1', 'key2']

2.删除对象属性:

  delete o.p;返回值为布尔类型,删除成功后返回true;  

   使用delete命令删除一个数组成员,会形成空位,并且不会影响length属性。

var o = {p: 1};
Object.keys(o) // ["p"]

delete o.p // true
o.p // undefined
Object.keys(o) // []

注意删除一个不存在的属性,delete不报错,而且返回true;

var o = {};
delete o.p // true

  delete命令只能删除对象本身的属性,无法删除继承的属性;

var o = {};
delete o.toString // true
o.toString // function toString() { [native code] }

  delete命令不能删除var命令声明的变量,只能用来删除属性。

var p = 1;
delete p // false
delete window.p // false

3.检查对象是否包含某个属性——in运算符

in运算符用于检查对象是否包含某个属性(注意,检查的是键名,不是键值),如果包含就返回true,否则返回false

in运算符,它不能识别对象继承的属性。

var o = { p: 1 };
'p' in o // true
var o = new Object();
o.hasOwnProperty('toString') // false  说明toString是继承属性

'toString' in o // true  in运算符不能识别,对继承的属性也返回true

 4.数组的length属性

数组的length值为数组最大的数字键+1

var a = [];
a[5] = 1;
console.log(a.length);//6

5.类似数组对象

类似数组的对象只有一个特征,就是具有length属性。换句话说,只要有length属性,就可以认为这个对象类似于数组。但是,对象的length属性不是动态值,不会随着成员的变化而变化。

var obj = {
  0: 'a',
  1: 'b',
  2: 'c',
  length: 3
};

obj[0] // 'a'
obj[2] // 'c'
obj.length // 3
obj.push('d') // TypeError: obj.push is not a function

典型的类似数组的对象是函数的arguments对象,以及大多数DOM元素集,还有字符串。

6.in运算符

检查某个键名是否存在,适用于对象,也适用于数组

键名都是字符串,如果数组的某个位置是空位,in运算符返回false

var arr = [ 'a', 'b', 'c' ];
console.log(2 in arr);  // true  由于键名都是字符串,所以数值2会自动转成字符串
console.log('2' in arr); // true
console.log(4 in arr); // false

与for...in的区别:

for...in不仅可以遍历数组的数字键,还可以遍历数组的非数字键

var a = [1, 2, 3];
a.foo = true;

for (var key in a) {
  console.log(key);
}
//0
//1
//2
//foo

in运算符只能遍历数组的数字键

var arr = [ 'a', 'b', 'c' ];
arr.foo = true;
console.log(2 in arr);  // true
console.log('2' in arr); // true
console.log(4 in arr); // false
console.log(foo in arr);//foo is not defined

7.从构造函数生成一个新对象

①.使用new命令

var o = new Person();//构造函数一般首字母大写

②.Object.create()

8.相等和严格等

“==”相等运算符会自动转换变量类型,造成很多意想不到的情况

不要使用相等,应使用严格相等

9.Object对象的实例方法

  • valueOf():返回当前对象对应的值。
    •     valueOf方法的作用是返回一个对象的“值”,默认情况下返回对象本身。
    • var o = new Object();
      o.valueOf() === o // true
        
  • toString():返回当前对象对应的字符串形式。toString()的应用:判断数据类型
    •   
      var o = {};
      o.toString() // "[object Object]"
      Object.prototype.toString.call(value);//通过函数的call方法,可以在任意值上调用Object.prototype.toString方法,帮助我们判断这个值的类型。

      不同数据类型的Object.prototype.toString方法返回值如下:

      • 数值:返回[object Number]
      • 字符串:返回[object String]
      • 布尔值:返回[object Boolean]
      • undefined:返回[object Undefined]
      • null:返回[object Null]
      • 数组:返回[object Array]
      • arguments对象:返回[object Arguments]
      • 函数:返回[object Function]
      • Error对象:返回[object Error]
      • Date对象:返回[object Date]
      • RegExp对象:返回[object RegExp]
      • 其他对象:返回[object Object]
        console.log(Object.prototype.toString.call(2)); // "[object Number]"
        console.log(Object.prototype.toString.call('')); // "[object String]"
        console.log(Object.prototype.toString.call(true)); // "[object Boolean]"
        console.log(Object.prototype.toString.call(undefined)); // "[object Undefined]"
        console.log(Object.prototype.toString.call(null)); // "[object Null]"
        console.log(Object.prototype.toString.call(Math)); // "[object Math]"
        console.log(Object.prototype.toString.call({})); // "[object Object]"
        console.log(Object.prototype.toString.call([])); // "[object Array]"

  • toLocaleString():返回当前对象对应的本地字符串形式。
  • hasOwnProperty():判断某个属性是否为当前对象自身的属性,还是继承自原型对象的属性。
  • isPrototypeOf():判断当前对象是否为另一个对象的原型。
  • propertyIsEnumerable():判断某个属性是否可枚举。

10.JSON格式

  1. 字符串必须使用双引号表示,不能使用单引号。

  2. 对象的键名必须放在双引号里面。

  3.  

    //以下是合格的JSON
    ["one", "two", "three"]
    
    { "one": 1, "two": 2, "three": 3 }
    
    {"names": ["张三", "李四"] }
    
    [ { "name": "张三"}, {"name": "李四"} ]

    以下是不合格的JSON:

    //以下是不合格的JSON
    { name: "张三", 'age': 32 }  // 属性名必须使用双引号
    
    [32, 64, 128, 0xFFF] // 不能使用十六进制值
    
    { "name": "张三", "age": undefined } // 不能使用undefined
    
    { "name": "张三",
      "birthday": new Date('Fri, 26 Aug 2011 07:13:10 GMT'),
      "getName": function() {
          return this.name;
      }
    } // 不能使用函数和日期对象

     

 

posted on 2017-06-12 22:21  我爱吃豌豆  阅读(186)  评论(0编辑  收藏  举报

导航