zoukankan      html  css  js  c++  java
  • js的原型对象和constructor研究

     1 <html>
     2     <head>
     3         <title>js的原型对象和constructor研究</title>    
     4     <head/>
     5     <body>
     6     
     7         <script >
     8             /**
     9             *javascript原型对象是一种特殊的实例,它提供一种在所有的实例中共享状态的机制
    10             *当访问对象成员时,先在当前对象中查找,,如果没有找到,则到该对象的原型对象中进行查找,,,一直到Object
    11             */
    12         
    13         //为Object的原型对象添加属性
    14         Object.prototype.sex = "male";
    15         
    16         function Base(){
    17             this.name = "eric";
    18         }
    19         
    20         Base.prototype = {
    21             constructor:Base,    //把原型对象的constructor指向Base
    22             name:"prototype..",
    23             greet: function(){ alert("hello...i'm  " + this.name + "and i m " + this.sex)}    //此处的sex继承自Object
    24         }
    25             
    26         var a = Base.prototype;
    27         a.greet();    //prototype
    28         var obj = new a.constructor;    //用原型对象的构造方法创建对象
    29         alert(obj.name);    //eric
    30         var b = new Base();
    31         b.greet();    // eric    
    32     
    33         </script>
    34 </body>
    35 </html>
  • 相关阅读:
    auto_ptr解析
    C++ auto_ptr智能指针的用法
    C++ 默认构造函数
    phpdisk 盲注 &前台任意用户登录
    冒泡排序
    关于C++中的友元函数的总结
    python中的闭包
    reverse Polish notation
    PostScript
    sqlite
  • 原文地址:https://www.cnblogs.com/playerlife/p/2626475.html
Copyright © 2011-2022 走看看