面向对象构建方法以及优缺点

面向对象
        // 1.对象字面量的方式创建对象
      
  var obj = {};
        // 使用场景:作为函数的参数,临时使用一次,

        obj.name = 'lisi';
        obj.say = function(){

        }
        var obj2 = {
            name:"wanger",
            age:12,
            say:function(){

            }
        }
        obj2.name
        obj2.say();
        缺点: 不能作为创建对象的模板,也就是不能使用new进行构造函数实例化




    ========================================================================

    var obj3=new object();
    obj3.name='kitty';
    obj3.hi=function(){

    }
    跟上面一样,只能临时用一下这个对象,不能作为模板
    ======================================================================

    function   Person(){
        this.name='123';
        this.age='23';
        this.say=function(){
            console.log()
    }
    var obj4=new Person();
    new关键字的作用:
        1.执行构造函数,在构造函数内部创建一个空对象
        2.将空对象与构造函数的原型对象进行关联
        3.将this执行这个空对象
        4.执行构造函数,返回新对象
    ===================================================================
     升级:把属性设置成参数

        function dot(){
            this.name=name||'';
            this.age=age||18;
        }
        dot.prototype={
            init:function(){

            }
        }
        var d=new dot('二狗zi',18)
     解决参数顺序问题

        function fish(option){
            this.name=option.name||'';
            this.age=option.age||'';
        }
        fish.prototype={
            init:function(){

            }
        }
     最终版
     function Pag(opation){
        this.init(opation)
     }
     pag.prototype={
        init:function(){
            this.name=option.name||'';
            this.age=option.age||18;
        }
        bindevent:function(){


        }
     }

猜你喜欢

转载自blog.csdn.net/Amourlwh/article/details/81543287
今日推荐