javascript prototype __proto__区别
An Object's __proto__ property references the same object as its internal [[Prototype]] (often referred to as "the prototype"), which may be an object or, as in the default case of Object.prototype.__proto__, null . This property is an abstraction error, because a property with the same name, but some other value, could be defined on the object too. If there is a need to reference an object's prototype, the preferred method is to use Object.getPrototypeOf.
A __proto__ pseudo property has been included in §B.3.1 of the draft ECMAScript ed. 6 specification (note that the specification codifies what is already in implementations and what websites may currently rely on).
var proto = obj.__proto__;
一个对象的__proto__ 属性和自己的内部属性[[Prototype]]指向一个相同的值 (通常称这个值为原型),原型的值可以是一个对象值也可以是null(比如说Object.prototype.__proto__的值就是null).该属性可能会引发一些错误,因为用户可能会不知道该属性的特殊性,而给它赋值,从而改变了这个对象的原型. 如果需要访问一个对象的原型,应该使用方法Object.getPrototypeOf.
__proto__ 属性已经被添加在了ES6草案 §B.3.1中.
不要认为__proto__和prototype相等。
Description
When an object is created, its __proto__ property is set to reference the same object as its internal [[Prototype]] (i.e. its constructor's prototype object). Assigning a new value to __proto__ also changes the value of the internal [[Prototype]] property, except where the object is non–extensible.
To understand how prototypes are used for inheritance, see the MDN article Inheritance and the prototype chain.
当一个对象被创建时,它的 __proto__ 属性和内部属性[[Prototype]]指向了相同的对象 (也就是它的构造函数的prototype属性).改变__proto__ 属性的值同时也会改变内部属性[[Prototype]]的值,除非该对象是不可扩展的.
想要知道如何使用原型来实现继承,查看MDN文章继承和原型链.
Example
In the following, a new instance of Employee is created, then tested to show that its __proto__ is the same object as its constructor's prototype.
// 声明一个函数作为构造函数function Employee() {
/* 初始化实例 */
}
// 创建一个Employee实例
var fred = new Employee();
// 测试相等性
fred.__proto__ === Employee.prototype; // true
这是, fred 继承了 Employee, 但是如果给fred.__proto__ 赋另外一个对象值,则会改变它的继承对象:
// Assign a new object to __proto__
fred.__proto__ = Object.prototype;
现在,fred不在继承于Employee.prototype, 而是直接继承了Object.prototype, 也就丢失了所有从Employee.prototype继承来的属性.
可是,这只适用于可扩展的 对象,一个不可扩展的对象的 __proto__ 属性是不可变的:
var obj = {};
Object.preventExtensions(obj);
obj.__proto__ = {}; // throws a TypeError
Note that even Object.prototype's __proto__ property can be redefined as long as the chain leads to null:
var b = {};
Object.prototype.__proto__ = {
hi: function () {alert('hi');},
__proto__: null
};
b.hi();
If Object.prototype's __proto__ had not been set to null, or had not been set to another object whose prototype chain did not eventually lead explicitly to null, a "cyclic __proto__ value" TypeError would result since the chain must eventually lead to null (as it normally does on Object.prototype).
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
上面的
bject.prototype.__proto__不能改成
bject.prototype.prototype.
为什么?
我们可以:
function func(){};
alert(typeof Object.prototype);//Object,不是Function
alert(typeof func); //Function。
可以看出Object.prototype是一个object,没有prototype属性.alert( Object.prototype.prototype);显示undefined。
(从这里我们可以看出Object 是 一个function,typeof Object 为function。)
prototype is a property of a Function object. It is the prototype of objects constructed by that function.
只有函数才有prototype属性,对象没有。
我们可以看stackoverflow上的一个问题:
This figure again shows that every object has a prototype. Constructor function Foo also has its own
__proto__which is Function.prototype, and which in turn also references via its__proto__property again to the Object.prototype. Thus, repeat, Foo.prototype is just an explicit property of Foo which refers to the prototype of b and c objects.
var b =new Foo(20);var c =new Foo(30);
What are the __proto__ and the prototype properties?

(要仔细理解这幅图 typeof Object=='function' 为true,说明Object类型为function。
Foo.prototype.__proto__ === Object.prototype 为true。
(个人理解:上面的b->Foo.prototype-->Object.prototype组成了一条链,但b没有在自己中找到相应的属性和方法时,就会向上去寻找 。我们可以这么理解,继承与prototype无关,而与__proto__有关。?我们在这里简单地说下。每个对象都会在其内部初始化一个属性,就是__proto__,当我们访问一个对象的属性时,如果这个对象内部不存在这个属性,那么他就会去__proto__里找这个属性,这个__proto__又会有自己的__proto__,于是就这样一直找下去,也就是我们平时所说的原型链的概念。
参考:http://www.cnblogs.com/youxin/archive/2013/03/08/2950751.html
可以看到,Function.prototype是函数Foo的__proto__。我们只要在Function.prototype增加了一个方法,所有的函数都可以调用这个方法,如《javascript精粹》中的一个例子:
Function.prototype.method=function(name,func){
if(!this.prototype[name]){
this.prototype[name]=func;
}
return this;
};
Foo.method("say2",function(){alert("say2");});
//和上面的话作用一样:Foo.prototype.say2=function(){alert("say2");};
var c=new Foo();
c.say2();
)
下面的这段代码是我编的。
function Foo(){}
var b=new Foo();
alert(Foo.prototype==Foo.__proto__); //false
alert( Foo.__proto__); //function Empty(){}
alert(Foo.prototype); //[object object]
alert(Foo.prototype.constructor); //function Foo(){}
与上图对应的代码:
// a constructor function
function Foo(y) {
// which may create objects
// by specified pattern: they have after
// creation own "y" property
this.y = y;
} // also "Foo.prototype" stores reference
// to the prototype of newly created objects,
// so we may use it to define shared/inherited
// properties or methods, so the same as in
// previous example we have: // inherited property "x"
Foo.prototype.x = 10; // and inherited method "calculate"
Foo.prototype.calculate = function (z) {
return this.x + this.y + z;
}; // now create our "b" and "c"
// objects using "pattern" Foo
var b = new Foo(20);
var c = new Foo(30); // call the inherited method
b.calculate(30); //
c.calculate(40); // // let's show that we reference
// properties we expect console.log( b.__proto__ === Foo.prototype, // true
c.__proto__ === Foo.prototype, // true // also "Foo.prototype" automatically creates
// a special property "constructor", which is a
// reference to the constructor function itself;
// instances "b" and "c" may found it via
// delegation and use to check their constructor b.constructor === Foo, // true
c.constructor === Foo, // true
Foo.prototype.constructor === Foo // true b.calculate === b.__proto__.calculate, // true
b.__proto__.calculate === Foo.prototype.calculate // true );
具体参考:http://dmitrysoshnikov.com/ecmascript/javascript-the-core/
答案1:
__proto__ is internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.getPrototypeOf(O) method, though de facto standard __proto__ is quicker.
You can find instanceof relationships by comparing a function's prototype to an object's__proto__ chain, and you can break these relationships by changing prototype.
function Point(x, y) {
this.x = x;
this.y = y;
}
var myPoint = new Point();
// the following are all true
myPoint.__proto__ == Point.prototype
myPoint.__proto__.__proto__ == Object.prototype
myPoint instanceof Point;
myPoint instanceof Object;
Here Point is a constructor function, it builds an object (data structure) procedurally. myPoint is an object constructed by Point() so Point.prototype gets saved to myPoint.__proto__ at that time.
答案2:
__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new:
(new Foo).__proto__ ===Foo.prototype
(newFoo).prototype ===undefined
转自:http://stackoverflow.com/questions/9959727/what-is-the-difference-between-proto-and-prototype-in-javascript
看以前写的:
javascript prototype __proto__区别的更多相关文章
- 关于 JavaScript prototype __proto__ 一点总结
http://www.cnblogs.com/wbin91/p/5265163.html 先上代码 function(y) Foo{ this.y = y;} Foo.prototype.x = 10 ...
- Javascript Prototype __proto__ constructor 三者的关系
JavaScript三大毒瘤 --- this,原型链,作用域 在我等菜鸟一步一步升级中的过程中,这三个概念总是困扰这我们(可能只有我吧,我比较蠢).这三个东西往往都很绕,今天我就来分享一下我对原型. ...
- Javascript深入__proto__和prototype的区别和联系
有一个一个装逼的同事,写了一段代码 function a(){} a.__proto__.__proto__.__proto__ 然后问我,下面这个玩意a.__proto__.__proto__.__ ...
- js中__proto__和prototype的区别和关系? 这样好理解多了
原型的概念 真正理解什么是原型是学习原型理论的关键.很多人在此产生了混淆,没有真正理解,自然后续疑惑更多. 首先,我们明确原型是一个对象,其次,最重要的是, Every function has a ...
- JavaScript中__proto__与prototype的关系
一.所有构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function) 1 2 3 4 5 6 7 8 9 Number.__proto__ ...
- js中__proto__和prototype的区别和关系?
_proto__(隐式原型)与prototype(显式原型)1.是什么 显式原型 explicit prototype property: 每一个函数在创建之后都会拥有一个名为prototype的属性 ...
- 深入理解JavaScript原型:prototype,__proto__和constructor
JavaScript语言的原型是前端开发者必须掌握的要点之一,但在使用原型时往往只关注了语法,其深层的原理并未理解透彻.本文结合笔者开发工作中遇到的问题详细讲解JavaScript原型的几个关键概念, ...
- JavaScript:Function/Object/prototype/__proto__
console.log(Object.__proto__===Function.prototype); //true console.log(Object.prototype.__proto__); ...
- JavaScript的__proto__、prototype和继承
JavaScript也是可以“继承”的! 各位看官或是好奇,或是一知半解.什么是prototype,__proto__,constructor.哪种继承方式好.今天就在这交流交流. 什么是protot ...
随机推荐
- Effective Java实作Comparable - 就是爱Java
当集合或数组内的对象需要排序时,会利用Collections.sort或Arrays.sort来进行排序,通常会implement Comparable,来实现自定义排序,透过回传值来表示排序的大小. ...
- Android 操作手机内置存储卡中的文件
场景:需要读取指定文件的内容,此文件是手动存储到手机内置存储卡中的,且手机上不存在SD卡. 对于android通过activity提供的openFileOutput和openFileInput可以直接 ...
- poj2975--Nim
题意:对于一个给定的取石子游戏,有多少种先手策略获胜? Ans:若无法获胜,则输出0. 若能获胜我们只要找到一堆石子,使得我们能取它的一部分让总和的异或和变为0.我们先将整个游戏的值异或起来为s 则a ...
- Red Hat Enterprise Linux 7的新功能
简介红帽最新版本的旗舰平台交付显著增强的可用性. 性能和可靠性. 丰富的新功能为架构. 系统管理员和开发人员提供所需的资源以更高效地进行创新和管理.架构师: 红帽® 企业 Linux® 7 适合 ...
- 【转】android cts failed items
原文网址:http://blog.csdn.net/linsa0517/article/details/19031479 Fail的一些修改 1.直接设置问题 estUnknownSourcesO ...
- Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum
这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...
- 《Java程序员面试笔试宝典》之 instanceof有什么作用
instanceof是Java语言中的一个二元运算符,它的作用是判断一个引用类型的变量所指向的对象是否是一个类(或接口.抽象类.父类)的实例,即它左边的对象是否是它右边的类的实例,返回boolean类 ...
- HTTP 协议实现
一.超文本传输协议及HTTP包 HTTP协议用于在Internet上发送和接收消息.HTTP协议是一种请求-应答式的协议--客户端发送一个请求,服务器返回该请求的应答,所有的请求与应答都是HTT ...
- hdu 4923 Room and Moor (单调栈+思维)
题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得 sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...
- Qt使用异或进行加密解密
在加密,解密中,异或运算应该时比较简单的一种.下面的代码,采用异或运算进行加密,解密: 点击(此处)折叠或打开 #include <QtCore/QCoreApplication&g ...