构造函数

2017-04-20  本文已影响0人  洛洛kkkkkk
<!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>
上一篇下一篇

猜你喜欢

热点阅读