首页 > 解决方案 > 对象在javascript中使用自己的方法

问题描述

对象可以定义属性以使用自己的方法吗?

点这个

obj = {
    prt:function(){console.log("HELLO")}
    prt0:obj.prt()
}

我想要 obj.prt0 -> "HELLO"

标签: javascriptfunctionobject

解决方案


var obj = {
    prt:function(){
        console.log("HELLO")
        return "HELLO";
    },
    prt0: function(){
        this.prt()
    }
}
obj.prt0;    //do nothing, it is only a method
obj.prt0();  //execute method, will print Hello

推荐阅读