简体   繁体   English

Javascript:Object或Object.prototype?

[英]Javascript: Object or Object.prototype?

I just learned about prototype in Javascript (not the framework, the native feature). 我只是学习了Java语言中的原型(不是框架,而是本机功能)。 I perfectly got, at least one of its use. 我完全理解了,至少使用了其中一种。 Example: 例:

Array.prototype.myMethod = function(){
   /*do something super awesome*/
}
var a = [];
a.myMethod();

Reading on I came upon an example in which the author adds a subClass method to the Object object, by doing this: 在继续阅读时,我看到了一个示例,其中作者通过执行以下操作向Object对象添加了subClass方法:

Object.subClass = function(hash){/*return an extended object that inherits Object's attributes*/}

The goal is to create a method that resembles a more object-oriented language syntax. 目标是创建一种类似于面向对象的语言语法的方法。 Since I expected the book author to define such method using the prototype feature my question is: 由于我希望本书的作者使用原型功能定义这种方法,因此我的问题是:

  • Why not use prototype ? 为什么不使用原型
  • Isn't more risky to add methods directly to the object rather than the prototype attached to it? 将方法直接添加到对象而不是附加到对象的原型是否更具风险?
  • Are there situations in which I'd prefer one way over the other 在某些情况下,我更喜欢一种方法

Why not use prototype? 为什么不使用原型?

Because if prototype is used then the subClass method is only available on instances of an Object . 因为如果使用了原型,则subClass方法仅在Object实例上可用。 For example, to call subClass the following would be necessary: 例如,要调用subClass ,需要以下步骤:

Object.prototype.subClass = function(hash) { /* return an extended object that inherits Object's attributes */ };
function MyClass() { };

var myInstance = new MyClass();
var mySubClassInstance = myInstance.subClass(); // only accessible on instances

This doesn't make much sense because the author wants subClass to return an extended instance of the object. 因为笔者想这并没有太大的意义subClass返回对象的扩展实例。 The intent is not to create an instance of the parent "class" and then return a new sub instance from that instance. 目的不是创建父“类”的实例,然后从该实例返回新的子实例。 That's unnecessary. 没必要

By defining it right on Object , the subClass instance can be created without first creating an instance of MyClass : 通过在Object正确定义它,无需先创建MyClass实例即可创建subClass实例:

Object.subClass = function(hash) { /* return an extended object that inherits Object's attributes */ };
function MyClass() { };

var mySubClassInstance = MyClass.subClass();

Isn't more risky to add methods directly to the object rather than the prototype attached to it? 将方法直接添加到对象而不是附加到对象的原型是否更具风险? Are there situations in which I'd prefer one way over the other. 在某些情况下,我更喜欢一种方法。

  • Add to prototype to add members to instances of that object. 添加到原型以将成员添加到该对象的实例。
  • Add to the object to add members to that object (similar to the idea of static members). 添加到对象以向该对象添加成员(类似于静态成员的想法)。

If you want method to be able to access class instance (with this keyword), use prototype. 如果希望方法能够访问类实例(使用this关键字),请使用prototype。 If you don't need this , declare function as member of class itself, so that you can call it without object. 如果不需要this ,则将function声明为类本身的成员,以便您可以在没有对象的情况下调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM