JavaScript 中原型面向对象编程的最佳方法
我更喜欢原型编程方式,并且一直在尝试用 javascript 来理解它。
我在 The Good Parts 中看到了这段代码:
function beget(o){
function F(){
F.prototype = o;
};
return new F();
};
我根本不明白,哈哈。如果您所要做的就是将原型设置为过去的对象,那么您不能这样做:
var parent = {
num = 66;
};
var child = {
prototype: parent
};
但这似乎不起作用,因为 child.num 返回为未定义。您如何描述 javascript 原型编程以及您的方法是什么? 谢谢大家
I rather like the prototype way of programming and have been trying to understand it in javascript.
I saw this bit of code in The Good Parts:
function beget(o){
function F(){
F.prototype = o;
};
return new F();
};
I don't get this at all lol. If all you have to do is set the prototype to a past object, then couldn't you just do this:
var parent = {
num = 66;
};
var child = {
prototype: parent
};
This doesn't seem to work though, cause child.num is returned as undefined. How do you describe javascript prototype programming and what are your methods?
Thanks guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我个人认为最惯用的方法如下:
Personally I think the most idiomatic method is as follows:
您只能将
prototype
添加到函数对象。当通过new
调用时,它将使用它作为原型。顺便说一句,您引用的函数是新版本 ECMAScript 的一部分
Object.create
(带有附加的propertiesObject
参数)。让我这样说:
{object}
是一个单例。然而,function() 对象
是一个构造函数,也就是说,当使用new
调用时会创建一个实例(通过执行函数的主体,并使用构造函数的原型)。当然原型也可以是一个函数对象,并且有自己的原型等。专业化和泛化意味着在原型链上走来走去。You can only add
prototype
to a function object. When invoked vianew
it will use it as its prototype.Btw, the function you quoted is part of the new version of ECMAScript as
Object.create
(with an additionalpropertiesObject
parameter).Let me put it this way:
{object}
is a singleton. Afunction() object
however is a constructor, that is, when called withnew
creates an instance (by executing the function's body, and using the constructor's prototype). Of course the prototype can be a function object too, and have its own prototype, etc. Specialization and Generalization means walking up and down the prototype chain.第二个示例不起作用,因为您只是设置对象的属性。
您想要做的是设置构造函数的原型属性,然后从该函数创建一个对象。
http://jsfiddle.net/W9C3K/
如果没有专有方法,则无法设置或更改对象的原型。
The second example does not work because your just setting a property of an object.
What you want to do is set the prototype property of a constructor function, and then create an object from that function.
http://jsfiddle.net/W9C3K/
Setting or changing the prototype on an object is not possible without proprietary methods.
.prototype
属性对 JavaScript 具有特殊含义,但仅适用于函数,这就是您的代码不起作用的原因。使用“原型继承”,您不会从基类继承。相反,您从某个对象开始并使用第一个对象作为原型创建一个新对象。然后向新创建的对象添加属性和方法。
我不确定你所说的“你的方法是什么?”是什么意思?您是指编程方法还是类似的东西?
更新:
你的问题相当广泛,我认为我不能在这里用一个简短的答案来公正地回答它。你可能想看看下面的SO问题,其中对JS原型有很好的讨论,以及一些链接其他人发现对这个主题有帮助的东西。
JavaScript .prototype 是如何工作的?
The
.prototype
property has special meaning to JavaScript, but only for functions, which is why your code doesn't work.With "protypical inheritance", you don't inherit from a base class. Instead, you start with with some object and create a new one, using the first as a prototype. Then you add properties and methods to the newly created object.
I'm not sure what you mean by "what are your methods?" Do you mean programming methodology, or something like that?
UPDATE:
Your question is pretty broad, and I don't think I can do it justice with a short answer here.You might want to have a look at the SO question below, which has a good discussion of JS prototypes, as well as some links to stuff that other people have found helpful on this topic.
How does JavaScript .prototype work?