Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

javascript - 使用“ Object.create”代替“ new”(Using “Object.create” instead of “new”)

Javascript 1.9.3 / ECMAScript 5 introduces Object.create , which Douglas Crockford amongst others has been advocating for a long time.(Javascript 1.9.3 / ECMAScript 5引入了Object.create ,这是Douglas Crockford等人提倡的很长时间了。)

How do I replace new in the code below with Object.create ?(如何使用Object.create替换下面代码中的new ?) 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(); (Assume MY_GLOBAL.nextId exists).((假设存在MY_GLOBAL.nextId )。) The best I can come up with is:(我能想到的最好的是:) 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(); There doesn't seem to be any advantage, so I think I'm not getting it.(似乎没有任何优势,所以我想我没有。) I'm probably being too neo-classical.(我可能太新古典了。) How should I use Object.create to create user 'bob'?(我应该如何使用Object.create创建用户“ bob”?)   ask by Graham King translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With only one level of inheritance, your example may not let you see the real benefits of Object.create .(仅具有一个继承级别,您的示例可能无法让您看到Object.create的真正好处。)

This methods allows you to easily implement differential inheritance , where objects can directly inherit from other objects.(通过此方法,您可以轻松实现差异继承 ,其中对象可以直接从其他对象继承。) On your userB example, I don't think that your init method should be public or even exist, if you call again this method on an existing object instance, the id and name properties will change.(在您的userB示例中,我认为您的init方法不应该是公共的,甚至不存在,如果您在现有对象实例上再次调用此方法,则idname属性将发生变化。) Object.create lets you initialize object properties using its second argument, eg:(Object.create允许您使用第二个参数来初始化对象属性,例如:) var userB = { sayHello: function() { console.log('Hello '+ this.name); } }; var bob = Object.create(userB, { 'id' : { value: MY_GLOBAL.nextId(), enumerable:true // writable:false, configurable(deletable):false by default }, 'name': { value: 'Bob', enumerable: true } }); As you can see, the properties can be initialized on the second argument of Object.create , with an object literal using a syntax similar to the used by the Object.defineProperties and Object.defineProperty methods.(正如可以看到,该属性可以被上的第二个参数进行初始化Object.create字面使用类似于由所使用的语法,与对象Object.definePropertiesObject.defineProperty方法。) It lets you set the property attributes ( enumerable , writable , or configurable ), which can be really useful.(它使您可以设置属性属性( enumerablewritableconfigurable ),这很有用。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...