使用“Object.create”而不是“New”
Object.createnewObject.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
慕桂英4014372
慕少森
随时随地看视频慕课网APP
相关分类