美文网首页
构造函数

构造函数

作者: 洛洛kkkkkk | 来源:发表于2017-04-20 19:33 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
        </body>
        <script type="text/javascript">
            //具有相同属性和方法的一组对象称为“类”
            
            //工厂模式
            
    //      function People (name,age) {
    //          var obj = new Object();
    //          obj.name = name;
    //          obj.age = age;
    //          obj.eat = function () {
    //              alert("吃饭");
    //          }
    //          obj.say = function () {
    //              alert("hello");
    //          }
    //          return obj;
    //      }
    //      var lili = People("lili",58);
    //      alert(lili.age);
    //      lili.say();
    
            //构造函数
            function People (name,age) {
                this.name = name;
                this.age = age;
                this.eat = function () {
                    alert("吃饭");
                }
                this.say = function () {
                    alert("hello");
                }
            }
    //      People.prototype = lili.__proto__
    //      lili.__proto__.constructor = People;
            People.prototype.run = function () {
                alert("跑起来");
            }
            
            //实例化对象,this指代的是实力化的对象
            var lili = new People("lili",58);
            console.log(lili);
    //      alert(lili.age);
            //实例化对象的时候,产生该对象的原型 __proto__
            console.log(lili.__proto__);
            
            var arr = new Array();
            console.log(arr.__proto__);
            
            
            
            
        </script>
    </html>
    

    相关文章

      网友评论

          本文标题:构造函数

          本文链接:https://www.haomeiwen.com/subject/ahalzttx.html