functionC(name){ this.name = name; } var a = new C('a'); console.log(a.name); // a;
//ES6 classC1{ constructor(name){ this.name = name; } } var b = new C1('b'); console.log(b.name); // b
//父类构建函数中的`this`也指向实例化的对像 classC2extendsC1{ constructor(name,age){ super(name); this.age = age; } } var c = new C2('c',10); console.log(c.name); // c console.log(c.age); // 10
call, apply 改变this指向
functionD(){ console.log('D:' + this); } var Obj = { name : 'obj', show : function(){ console.log('name:' + this.name); } }