Javascript中的“是什么”意味着什么? [英] What does 'is an instance of' mean in Javascript?

查看:101
本文介绍了Javascript中的“是什么”意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的答案:
JavaScript函数的prototype属性的初始值是什么?



有这样一句话:


任何新创建的Function实例的原型初始值都是Object的新实例

据我所知,Javascript没有类,因此'实例'这个词在我的脑海中没有意义。如何在JavaScript中解释'实例'?



对不起,我没有足够的代表将问题放在评论线索中。

解决方案

你说得对,JavaScript没有类(但是),但它具有构造函数,定义对象和构造函数之间关系的instanceof 运算符,以及基于原型链的继承形式。



obj当 ctor.prototype 位于 obj 的原型链上时,instanceof ctor 为true。 / p>

在下面的解释中,您可以在EcmaScript 5中实现 instanceof ,因此

 函数isInstanceOf(obj,ctor){
var proto = ctor.prototype;
if(typeof obj ===object|| typeof obj ===function){
while(obj){
if(obj === proto){return true ; }
obj = Object.getPrototypeOf(obj);
}
}
返回false;

$ / code>

除非您重新分配原型( o = new MyConstructor(); MyConstructor.prototype = somethingElse )应该是这样的情况: new MyConstructor()instanceof MyConstructor



部分 15.3.5.3 详细解释了这一点。


15.3.5.3 [[HasInstance]](V)



假设F是一个函数对象。



当F的[[HasInstance]]内部方法用值V调用时,采取以下步骤:如果V不是对象,则返回false。

  • 设O为调用的结果如果Type(O)不是Object,则抛出一个TypeError异常。

  • 重复


    1. 设V为V的[[Prototype]]内部属性的值。

    2. 如果V为null,则返回false。
    3. 如果O和V引用同一个对象,则返回true。

    >



  • 这不是全部内容,因为主对象(如DOM节点)尽管他们喜欢实现 [[HasInstance]] 内部方法,但大多数浏览器都实现了主机对象,以尽可能接近本机对象。


    The answer to this question: What is the initial value of a JavaScript function's prototype property?

    has this sentence:

    The initial value of prototype on any newly-created Function instance is a new instance of Object

    As far as I know, Javascript doesn't have classes and the word 'instance' therefor doesn't make sense in my head. How should one interpret 'instance' in Javascript?

    Sorry, I don't have enough rep to put my question in the comment thread on that answer.

    解决方案

    You're right that JavaScript doesn't have classes (yet), but it does have constructor functions, an instanceof operator that defines a relationship between objects and constructors, and a form of inheritance based on prototype chains.

    obj instanceof ctor is true when ctor.prototype is on obj's prototype chain.

    Modulo the caveat below, you could implement instanceof in EcmaScript 5 thus

    function isInstanceOf(obj, ctor) {
      var proto = ctor.prototype;
      if (typeof obj === "object" || typeof obj === "function") {
        while (obj) {
          if (obj === proto) { return true; }
          obj = Object.getPrototypeOf(obj);
        }
      }
      return false;
    }
    

    Unless you go around reassigning prototypes (o = new MyConstructor(); MyConstructor.prototype = somethingElse) it should be the case that new MyConstructor() instanceof MyConstructor.

    Section 15.3.5.3 explains this in detail.

    15.3.5.3 [[HasInstance]] (V)

    Assume F is a Function object.

    When the [[HasInstance]] internal method of F is called with value V, the following steps are taken:

    1. If V is not an object, return false.
    2. Let O be the result of calling the [[Get]] internal method of F with property name "prototype".
    3. If Type(O) is not Object, throw a TypeError exception.
    4. Repeat

      1. Let V be the value of the [[Prototype]] internal property of V.
      2. If V is null, return false.
      3. If O and V refer to the same object, return true.

    This isn't the whole story because host objects (like DOM nodes) are allowed to implement the [[HasInstance]] internal method however they like but most browsers implement host objects to behave as closely to native objects as possible.

    这篇关于Javascript中的“是什么”意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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