试图了解新关键字 [英] Trying to understand the new keyword

查看:51
本文介绍了试图了解新关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确切地了解javascript new关键字的含义,为什么必要以及何时使用它.

I'm trying to understand exactly what the javascript new keyword means, why it is necessary and when I should use it.

请考虑以下示例:

      var x = new function(){
        var self=this;
        this.myFunction = function(){alert('foo ' + self.v)}
        this.v='x';
      };

      var y = function(){
        var self=this;
        this.myFunction = function(){alert('foo ' + self.v)}
        this.v='y';
        return this;
      }();


      var f=function(){
        var self=this;
        this.myFunction = function(){alert('foo ' + self.v)}
        this.v='z';
      }

      var z = new f();

      x.myFunction();
      y.myFunction();
      z.myFunction();

x,y和z都是对象.可能具有公共和私有成员变量.

x,y and z are all objects. Potentially with public and private member variables.

x和z使用new关键字构造.据我所知,new关键字只是执行函数并返回上下文.所以我想在上面的示例中x和y本质上是单例,因为对原始函数的任何引用都丢失了?

x and z where constructed with the use of the new keyword. As far as I can tell, the new keyword simply executes the function and returns the context. So I guess in the above example x and y are essentially singletons since any reference to the original function is lost?

除此之外,它们是否完全相同?如果不是,则有什么区别?我什么时候想使用一种方法或避免使用另一种方法?

Apart from that are they all exactly equivalent? If not what are the differences and when would I want to use one approach or avoid another?

谢谢您的解释.

推荐答案

我试图准确地理解javascript new关键字的含义

I'm trying to understand exactly what the javascript new keyword means

a = new X()的含义类似于:

  • 创建新对象A
  • 将其原型设置为名为X.prototype的对象(所有函数都有一个"prototype"属性,该属性只是一个对象,您可以对其进行修改,以将行为添加到使用此函数和new创建的对象中)
  • 使用this = a调用X以初始化对象.
  • create new object A
  • set its prototype to the object called X.prototype (all functions have a "prototype" property which is just an object and you can modify it to add behaviour to objects created using this function and new),
  • call X with this=a to initialize the object.

这类似于:

a = Object.create(/* prototype */ X.prototype, {});
a.constructor = X; /* magic property which can be read after */
X.apply(/* this */ a, /* args */[]);

为什么有必要以及何时应该使用它.

why it is necessary and when I should use it.

不是,您也不必这样做. Object.create与基于JS原型的对象模型一起使用时更为自然,并允许您创建任意原型链.

It isn't and you don't have to. Object.create works more naturally with JS prototype-based object model and lets you create arbitrary prototype chains.

使用new时,其用法如下:

function Class(constructor_args) {
   // init an object
   this.a = 10;
};
Class.prototype.method1 = function() {
   console.log(this.a);
}
var obj = new Class();

然后,在使用new Class()创建对象之后,其原型将引用对象Class.prototype,该对象包含其方法,而对象本身包含字段.

Then after you create an object using new Class(), its prototype refers to the object Class.prototype which contains its methods while the object itself contains the fields.

(个人观点:引入关键字new是为了使Javascript更容易编写类似于Java/C ++和经典非原型对象模型的代码.)

(Personal opinion: The keyword new was introduced to make it easier in Javascript to write code which resembles Java/C++ and classical non-prototypal object model.)

请阅读以下优秀文章:

JavaScript中的经典继承

原型继承

这篇关于试图了解新关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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