javascript 中的 __proto__ 和 prototype

作者: admin 日期: 2018-01-30 17:32:53 人气: - 评论: 0

prototype是javascript中一个特殊的概念,只要是一个函数对象就有prototype属性,

因为javascript中的函数可以作为一个对象的构造器来创建一个对象,所以一个函数的prototype主要会影响到以这个函数为构造器生成的对象

function Func(){}
var test = new Func()
Func.prototype.a="hello"
test.a //hello

函数的原型对象默认子包含一个指向函数构造函数的constructor属性


如果是普通对象只有__proto__属性而没有prototype属性在一般情况下__proto__属性指向了对象的构造函数的prototype属性

function Func(){}
var test = new Func()
test.__proto__.a=1
test.a //a
Func.prototype //a

以上代码说明 test.__proto__ 和 Func.prototype 其实指向的是同一个东西



function Func(){}
var test1 = new Func()
var test2 = new Func()
test1.__proto__.a=1
test2.a  //1

这里test1对象和test2对象都是从Func函数new出来的算是兄弟对象通过__proto__居然可以访问到同一块数据

相关内容

发表评论
更多 网友评论0 条评论)
暂无评论

Copyright © 2012-2014 我的代码板 Inc. 保留所有权利。

页面耗时0.0230秒, 内存占用1.82 MB, 访问数据库13次

闽ICP备15009223号-1