Open Bless-L opened 8 years ago
var obj = new foo(); //相当于 var obj = {}; obj.__proto__ = foo.prototype;
setTimeout中,如果回调函数使用匿名函数,则作用域为全局作用域,其this指向window,如果只是代码段,则作用域为当前执行的作用域。
例如:
var x=1; foo = function(){ this.x=2; this.set =function(){ setTimeout(console.log(this.x),0) } this.sett = function(){ setTimeout(function(){ console.log(this.x) },1000) } } var f = new foo(); f.set(); //输出2 f.sett();//输出1
JavaScript实现类继承的主要方式是原型继承。
方法一:new一个父类对象并把它赋值给子类对象的prototype 属性。 方法二:运用Object.create方法和constructor方法,缺点是ES5才支持。
JavaScript的一些语法细节
setTimeout中,如果回调函数使用匿名函数,则作用域为全局作用域,其this指向window,如果只是代码段,则作用域为当前执行的作用域。
例如:
JavaScript实现类继承的主要方式是原型继承。
方法一:new一个父类对象并把它赋值给子类对象的prototype 属性。 方法二:运用Object.create方法和constructor方法,缺点是ES5才支持。