浅谈工作中 代码的编写

在工作中,对代码的书写是由些讲究的,所以方法的书写在团队中也是很谨慎的,避免方法重名覆盖

// 将方法写入对象中
var pf = {
    eat:function(){
        console.log("对象eat")
    },
    say:function(){
        console.log("对象say")
    }
}
pf.eat()

// 将方法写入函数中返回出来
var pfzz = function(){
    return {
        eat:function(){
            console.log("函数返回eat")
        },
        say:function(){
            console.log("函数返回say")
        }
    }
}

var a = pfzz()
a.eat()

// 将函数看做对象,在将方法写入
var pfz = function(){
        this.eat = function(){
            console.log("函数对象eat")
        },
        this.say = function(){
            console.log("函数对象say")
        }
}

var b = new pfz()
b.eat()

// 将函数看做对象,在将方法写入到对象的原型链中
var pfzzz = function(){}
pfzzz.prototype = {
    eat : function(){
        console.log("对象原型链eat")
    },
    say : function(){
        console.log("对象原型链say")
    }
}

var c = new pfzzz()
c.eat()

个人浅见,望指正。
(学习随笔4.14)

猜你喜欢

转载自blog.csdn.net/qq_39305051/article/details/89309739
今日推荐