zoukankan      html  css  js  c++  java
  • prototype 原型

    在我刚学习javascript的时候对于原型的概念很模糊,现在也不是很懂,希望下面的文章对有相同问题的朋友有帮助,如果有误希望指正:

    prototype用于通常用于构造函数中,公用方法的继承上。构造函数时类的共有标识。在构造函数的原型上定义方法,通过构造函数实例来的实例函数,都具有构造函数原型中定义的方法。

    每一个函数都有一个prototype属性,这个属性的值是一个对象。

    这个对象只有一个不可枚举的属性constructor,这个属性的值一个函数对象: varF=function(){};

    <script>
        var F=function(){}; 
        var p=F.prototype; //F的原型函数
        var c=p.constructor; //构造函数
        alert(c===F); //true 对于任意函数的prototype.constructor;
    </script>

    构造函数的原型中存在预先定义好的constructor属性,这意味着对象通常继承的constructor均代指他们的构造函数。构造函数是类的‘公共标示’,因此这个constructor属性为对象提供了类。

    <script>
        var F=function(){}; 
        var o=new F();
        alert(o.constructor==F); //true
    </script>

    构造函数对象:定义了类的名字。

    原型对象:添加属性是所有实力共享。

    实例对象:添加属性是自己的。

  • 相关阅读:
    Leetcode#179 Largest Number
    Leetcode#155 Min Stack
    Leetcode#14 Longest Common Prefix
    Leetcode#101 Symmetric Tree
    Leetcode#172 Fractorial Trailing Zero
    Leetcode#28 Implement strStr()
    Leetcode#46 Permutations
    Leetcode#48 Rotate Image
    Leetcode#134 Gas station
    Leetcode#137 Single Number II
  • 原文地址:https://www.cnblogs.com/gaidalou/p/5993713.html
Copyright © 2011-2022 走看看