Js 原型链

var a = {}

console.log(a.__proto__=== Object.prototype); // true

 

var A = function(){}
var a = new A();

console.log(a.__proto__=== A.prototype); // true

var a1 = {}
var a2 = Object.create(a1);

console.log(a2.__proto__ === a1); //true

JavaScript 只有一种结构:对象。每个实例对象( object )都有一个私有属性(称之为 __proto__ )指向它的构造函数的原型对象prototype )。该原型对象也有一个自己的原型对象( __proto__ ) ,层层向上直到一个对象的原型对象为 null。根据定义,null 没有原型,并作为这个原型链中的最后一个环节 

console.log(a.__proto__ === a.constructor.prototype); //true
console.log(Person.prototype.constructor === Person); //true
console.log(Person.constructor.prototype === Person); //false
function Person() {}

console.log(Person.constructor.prototype ); // function () { [native code] }

console.log(Person.constructor ); // function Function() { [native code] }

console.log(Person ); // function Person() {}

普通对象与函数对象

var o1 = {}; 
var o2 =new Object();
var o3 = new f1();

function f1(){}; 
var f2 = function(){};
var f3 = new Function('str','console.log(str)');

console.log(typeof Object); //function 
console.log(typeof Function); //function  

console.log(typeof f1); //function 
console.log(typeof f2); //function 
console.log(typeof f3); //function   

console.log(typeof o1); //object 
console.log(typeof o2); //object 
console.log(typeof o3); //object

普通对象的constructor 指向function Object() { [native code] }

函数对象的constructor 指向function Function() { [native code] }

function Person(name, age, job) {
 this.name = name;
 this.age = age;
 this.job = job;
 this.sayName = function() { alert(this.name) } 
}
 var person1 = new Person('Nid', 25, 'Killer');
 var person2 = new Person('Hogg', 23, 'Doctor');

 console.log(person1.constructor === Person); //true
 console.log(person2.constructor === Person); //true
var b = new Array();
b.constructor === Array;
b.__proto__ === Array.prototype;

var c = new Date(); 
c.constructor === Date;
c.__proto__ === Date.prototype;

var d = new Function();
d.constructor === Function;
d.__proto__ === Function.prototype;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值