Javascript原型和__proto__以及getPrototypeOf问题 [英] Javascript prototype and __proto__ and getPrototypeOf issue

查看:127
本文介绍了Javascript原型和__proto__以及getPrototypeOf问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中有一个简单的类:

I have a simple class in javascript:

function foo() {
    this.bar = "bar";
}

var test = new foo;

console.log(foo.prototype,foo.__proto__)
/*output: foo {
               constructor: function foo() { 
               __proto__:   Object
              }
          ,
          function Empty() {}
*/
console.log(test,test.prototype,test.__proto__,test.__proto__.__proto__)
/*output: foo {
              bar: "bar"
              __proto__: foo
          }
          ,
          undefined
          ,
          foo {
              constructor: function foo() {
              __proto__: Object
          }
          ,
          Object {
              ...
          }
      */

我不明白:

在第一个日志中, foo.prototype 具有 __ proto __ 属性,该属性为对象
在第二个日志中, test .__ proto __ 具有 __ proto __ 属性whi ch是对象

At the first log the foo.prototype had the __proto__ attribute which was an Object At the second log the test.__proto__ had the __proto__ attribute which was an Object

何时使用 __ proto __ 和当原型时,有什么区别?

When to use __proto__ and when prototype, what is the difference?

更新:

instanceOf 部分的 John Resig的博客 有些事情我不明白:

In John Resig's blog in the instanceOf section there is something waht I don't understand:

如果我使用 var asd =asd; Object.getPrototypeOf (asd)它抛出 TypeError 但是

如果我使用 var dsa = new String(dsa); Object.getPrototypeOf (dsa)它返回 String

asd.constructor.prototype == dsa.constructor.prototype true

If i use var asd = "asd";Object.getPrototypeOf(asd) it throws a TypeError but
If i use var dsa = new String("dsa");Object.getPrototypeOf(dsa) it returns String
But asd.constructor.prototype == dsa.constructor.prototype is true

为什么 Object.getPrototypeOf (asd)抛出错误?这是一个字符串,不是吗?

Why is the Object.getPrototypeOf(asd) throws error? This is a string, isn't it?

推荐答案

始终使用原型 Object.getPrototypeOf

Always use prototype or Object.getPrototypeOf.

__ proto __ 是非标准的,已被 Mozilla

__proto__ is non-standard and has been deprecated by Mozilla.

John Resig有一个很好的博客文章关于它。

John Resig has a good blog entry about it.

test.prototype 之所以未定义是因为你创建了一个没有构造函数原型的新对象。这是一个何时使用 Object.getPrototypeOf 的示例。

The reason why test.prototype is undefined is because you created a new object that does not have a constructor prototype. Here's an example of when to use Object.getPrototypeOf.

js> function foo(){}
js> typeof foo.prototype;
object
js> var f = new foo();
js> typeof f.prototype;
undefined
js> typeof Object.isPrototypeOf(f);
object
js> typeof f.constructor.prototype;
object
js> foo.prototype === Object.getPrototypeOf(f);
true
js> foo.prototype === f.constructor.prototype;
true






如你所知, JavaScript中的继承很棘手。让我们看一个例子:


As you can tell, inheritance in JavaScript is tricky. Lets look at an example:

js> typeof "asdf";
string
js> typeof String("asdf");
string

字符串文字的类型为字符串。将 String()作为函数调用时也是如此。现在,由于 new 运算符的行为,创建了一个新对象,其原型为 String() as它的父母。

The string literal is of type string. The same is true when calling String() as a function. Now, because of behavior of the new operator, a new object is created, with the prototype of String() as its parent.

js> var s = new String("asdf");
js> typeof s;
object

因为JS喜欢强迫事情,你可以得到一些字符串文字方式:

Because JS likes to coerce things, you can get at the string literal a few ways:

js> s
asdf
js> s.valueOf();
asdf
js> typeof s
object
js> typeof s.valueOf();
string

JavaScript中的Prototypal继承帮助了我很多。

来自Mozilla的字符串页面:

From Mozilla's Strings page:


字符串对象可以由
创建,调用构造函数new String()。
String对象使用下面描述的
方法包装JavaScript的
字符串原语数据类型。全局
函数String()也可以在没有new的情况下调用
来创建
原始字符串。
中的字符串文字JavaScript是原始字符串。

String objects may be created by calling the constructor new String(). The String object wraps JavaScript's string primitive data type with the methods described below. The global function String() can also be called without new in front to create a primitive string. String literals in JavaScript are primitive strings.

这篇关于Javascript原型和__proto__以及getPrototypeOf问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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