Object对象

属性判断

  • in

in可以判断一个对象是否存在某个属性。

var xiaoming = {
    name: '小明',
    birth: 1990,
};
'name' in xiaoming; // true
'grade' in xiaoming; // false

但是对于继承的属性,也同样判断为true

'toString' in xiaoming; // true
  • hasOwnProperty()

hasOwnProperty()可以判断一个属性是否自身拥有,而不是继承。

Object.keys方法和Object.getOwnPropertyNames方法都可以遍历对象自身的属性,返回数组,只是后者还返回不可枚举的属性。

var o = {
  p1: 123,
  p2: 456
};

Object.keys(o)
// ["p1", "p2"]

Object.getOwnPropertyNames(o)
// ["p1", "p2"]

this

在函数内部定义的this,指向window;

'use strict';严格模式下,this指向undefined

绑定到对象上的函数称为方法,方法上的this,指向这个对象;

如何在方法函数里面再定义函数,this指向window

apply()、call()

这两个方法可以改变函数的this指向。

  • apply()把参数打包成Array再传入;

  • call()把参数按顺序传入。

对普通函数调用,我们通常把this绑定为null

Last updated

Was this helpful?