JavaScript 中原型面向对象编程的最佳方法

发布于 2024-11-05 17:48:27 字数 403 浏览 5 评论 0原文

我更喜欢原型编程方式,并且一直在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

糖果控 2024-11-12 17:48:27

我个人认为最惯用的方法如下:

function Parent() {
    this.value = 2;
}

function Child() {

}

Child.prototype = new Parent();

var c = new Child();
alert(c instanceof Child); // true
alert(c instanceof Parent); // true
alert(c.value); // 2

Personally I think the most idiomatic method is as follows:

function Parent() {
    this.value = 2;
}

function Child() {

}

Child.prototype = new Parent();

var c = new Child();
alert(c instanceof Child); // true
alert(c instanceof Parent); // true
alert(c.value); // 2
无可置疑 2024-11-12 17:48:27

您只能将prototype 添加到函数对象。当通过 new 调用时,它将使用它作为原型。

顺便说一句,您引用的函数是新版本 ECMAScript 的一部分 Object.create(带有附加的propertiesObject参数)。

让我这样说:{object} 是一个单例。然而,function() 对象 是一个构造函数,也就是说,当使用new 调用时会创建一个实例(通过执行函数的主体,并使用构造函数的原型)。当然原型也可以是一个函数对象,并且有自己的原型等。专业化泛化意味着在原型链上走来走去。

You can only add prototype to a function object. When invoked via new 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 additional propertiesObject parameter).

Let me put it this way: {object} is a singleton. A function() object however is a constructor, that is, when called with new 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.

土豪 2024-11-12 17:48:27

第二个示例不起作用,因为您只是设置对象的属性。
您想要做的是设置构造函数的原型属性,然后从该函数创建一个对象。

var parent = {
   num : 666
};

function ChildConstructor(){
   console.log(this.num);
}
ChildConstructor.prototype = parent;

var child = new ChildConstructor();

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.

var parent = {
   num : 666
};

function ChildConstructor(){
   console.log(this.num);
}
ChildConstructor.prototype = parent;

var child = new ChildConstructor();

http://jsfiddle.net/W9C3K/

Setting or changing the prototype on an object is not possible without proprietary methods.

臻嫒无言 2024-11-12 17:48:27

.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?

放肆 2024-11-12 17:48:27
var parent = {
  num : 66
};
var child = {
   __proto__: parent
};
var parent = {
  num : 66
};
var child = {
   __proto__: parent
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文