JavaScript 语言中的 this 详解

103 阅读1分钟

1、涵义

this 是属性或方法“当前”所在的对象

this.name

上面代码中,this 就代表 name 属性当前所在的对象。

下面是一个实际的例子:


var people = {
    name="wangyan",
    describe:function() {
      return '姓名:' + this.name;
    }
}

people.describe()

上面代码中,this.name 表示 name 属性所在的那个对象。由于 this.name 是在 describe 方法中调用,而 describe 方法所在的对象就是 people,因此 this 指向 people, this.name 就是 people