Javascript:对象还是Object.prototype? [英] Javascript: Object or Object.prototype?

查看:50
本文介绍了Javascript:对象还是Object.prototype?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解了 Javascript 中的原型(不是框架,原生功能).我完全得到了,至少它的一种用途.示例:

Array.prototype.myMethod = function(){/*做一些超级棒的事情*/}var a = [];a.myMethod();

继续阅读我发现了一个例子,其中作者向 Object 对象添加了一个 subClass 方法,这样做:

Object.subClass = function(hash){/*返回一个继承Object属性的扩展对象*/}

目标是创建一种类似于更面向对象的语言语法的方法.由于我希望本书作者使用原型功能定义此类方法,因此我的问题是:

  • 为什么不使用原型?
  • 直接向对象添加方法而不是附加到它的原型不是更有风险吗?
  • 在某些情况下,我更喜欢一种方式而不是另一种方式

解决方案

为什么不使用原型?

因为如果使用原型,则 subClass 方法仅适用于 Object 的实例.例如,要调用 subClass 需要以下内容:

Object.prototype.subClass = function(hash) {/* 返回一个继承 Object 属性的扩展对象 */};函数 MyClass() { };var myInstance = new MyClass();var mySubClassInstance = myInstance.subClass();//只能在实例上访问

这没有多大意义,因为作者希望 subClass 返回对象的扩展实例.目的不是创建父类"的实例,然后从该实例返回一个新的子实例.这是不必要的.

通过在 Object 上正确定义它,subClass 实例可以在不首先创建 MyClass 的实例的情况下创建:

Object.subClass = function(hash) {/* 返回一个继承 Object 属性的扩展对象 */};函数 MyClass() { };var mySubClassInstance = MyClass.subClass();

<块引用>

直接向对象添加方法而不是附加到它的原型不是更有风险吗?在某些情况下,我更喜欢一种方式而不是另一种方式.

  • 添加到原型以向该对象的实例添加成员.
  • 添加到对象以向该对象添加成员(类似于静态成员的想法).

I just learned about prototype in Javascript (not the framework, the native feature). 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 = 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. For example, to call subClass the following would be necessary:

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

这篇关于Javascript:对象还是Object.prototype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆