使用“Object.create”而不是“New”
Object.create
new
Object.create
?
var UserA = function(nameParam) { this.id = MY_GLOBAL.nextId(); this.name = nameParam;}UserA.prototype.sayHello = function() { console.log('Hello '+ this.name);}var bob = new UserA('bob');bob.sayHello();
MY_GLOBAL.nextId
var userB = { init: function(nameParam) { this.id = MY_GLOBAL.nextId(); this.name = nameParam; }, sayHello: function() { console.log('Hello '+ this.name); }};var bob = Object.create(userB);bob.init('Bob');bob.sayHello();
Object.create
慕少森
相关分类