1
var o = new Object() var o = new Object // 如果没有参数,括号可以省略
2
this.init.apply(this, arguments) ???
// 创建类 var Class = function() { var klass = function() { this.init.apply(this, arguments) } klass.prototype.init = function() {} return klass } var Person = new Class() Person.prototype.init = function() { } var person = new Person()
what does this usage of apply() means in Javascript
;(function() { var Klass = function(arg1, arg2, arg3) { this.init(arg1, arg2, arg3) } Klass.prototype.init = function(arg1, arg2, arg3) { console.log(arg1, arg2, arg3) } var klass = new Klass(1, 2, 3) })() ;(function() { var Klass = function() { this.init.apply(this, arguments) } Klass.prototype.init = function() { console.log(arguments[0], arguments[1], arguments[2]) } var klass = new Klass(1, 2, 3) })() ;(function() { var Klass = function() { this.init(arguments[0], arguments[1], arguments[2]) } Klass.prototype.init = function() { console.log(arguments[0], arguments[1], arguments[2]) } var klass = new Klass(1, 2, 3) })()
3
/** * 当你读取一个对象的属性时,JS首先会在本地对象中查找这个属性, * 如果没有找到,JS开始在对象的原型中查找, * 仍未找到还会继续查找原型的原型, * 直到查找到Object。prototype * 如果找到这个属性,则返回这个值,否则返回undefined */ var Person = function() { this.say = function() { console.log('e') } } Person.prototype.say = function() { console.log('ee') } new Person().say() // e