原型与原型链详解
发布日期:2021-05-10 03:19:19 浏览次数:24 分类:精选文章

本文共 4221 字,大约阅读时间需要 14 分钟。

���������������������������������

������������JavaScript��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� instanceof ���������������������������������������������������


1. ������������������������

������������������������������������������������ (__proto__) ������������������������������������������������������ prototype������������������������������������������������������������������������ __proto__ ���������������������������������������������������������

  • ������������������������������������������������������ prototype ������������������������������ Object ���������
  • ��������������������������������������� __proto__ ������������������������������������������

2. ������������ (prototype)

2.1 ���������������������������

��������������� prototype ���������������������������������������������������������������

function Fun() {}
console.log(Fun.prototype); // ������: {}

������������������������������������ Object ���������������������������������������������������������������������

Fun.prototype.test = function() {
console.log('������������������������');
};

2.2 ���������������������

������������������������������������������������������. ���������

Function.prototype ComponentFixture = function() {
console.log('������������������');
};
console.log(Function.prototype); // ������������ `FixComponent` ������

2.3 ���������������������������

��������������������������������������������� constructor ���������������������������������������������������

functionFun() {}
console.log(Fun.prototype.constructor); // ������: functionFun

3. ������������ (__proto__)

������������������������������������ __proto__������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������
    function A() {}
    A.prototype.n = 1; // ������������������
    var obj1 = new A();
    A.prototype = {n: 2, m: 3}; // ������������
    var obj2 = new A();
    console.log(obj1.n, obj1.m, obj2.n, obj2.m); // 1, undef, 2, 3

4. ������������������������

��������������������������������������������������������������������������������������������������������������� Object.prototype������������������������������ undefined���

function Fun() {}
Fun.prototype.test1 = function() {
console.log('test1');
}
Fun.prototype.test2 = function() {
console.log('test2');
}
var fun = new Fun();
fun.test1(); // test1
fun.test2(); // test2
fun.toString(); // [object Object]
fun.test3(); // undefined

5. instanceof ���������������

instanceof ������������������������������������������������������������������������������������������������������������������������������

  • ������������������ A instanceof B ������������������ A.__proto__ === B.prototype���

  • ���������������

    console.log(Function instanceof Object); // true
    console.log(Object instanceof Object); // true
    console.log(Function instanceof Function); // true
    console.log(Function instanceof Object); // true
    console.log(Object.prototype instanceof Object); // false
  • ��������������� Foo instanceof Object ������ false��������� Object.prototype ��������� Foo ������������


6. ������������������

������������

// ������������
function A() {}
A.prototype.n = 1;
var b = new A();
A.prototype = { n: 2, m: 3 };
var c = new A();
console.log(b.n, b.m, c.n, c.m); // 1, undef, 2, 3

���������b.n ��� 1���b.m ��� undefined���c.n ��� 2���c.m ��� 3���


������������

// ������������
function F() {}
Object.prototype.a = function() { console.log('a()'); };
Function.prototype.b = function() { console.log('b()'); };
var f = new F();
f.a(); // ���������a()
f.b(); // ���������b()
F.a(); // ���������a()
F.b(); // ���������b()

������������������������������������������������������������������������


������

������������JavaScript������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:前端笔试题总结(一) - JS篇
下一篇:CSS3 calc() 用法

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月07日 09时03分16秒