javascript - 对象在javascript中使用自己的方法
问题描述
对象可以定义属性以使用自己的方法吗?
点这个
obj = {
prt:function(){console.log("HELLO")}
prt0:obj.prt()
}
我想要 obj.prt0 -> "HELLO"
解决方案
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
推荐阅读
- reactjs - 未收到 redux 表单验证
- javascript - 在滚动时松开的两个功能之间添加延迟
- mongodb - 如何使用聚合和按两个字段分组
- java - 尝试使用 Bean Validation 验证字段时没有任何反应
- python-3.x - facetgrid中的keyerror索引
- string - 将参数值从字符串转换为日期时间失败(Visual Studio 到 Access 数据库)
- c++ - 为什么这些自定义的 new 和 delete 分配器的行为是这样的?
- c - 你如何限制你在一个字符串中扫描的字母数量?
- max - 使用 $max 更新 MongoDB 集合文档
- twilio - 一分钟后,Twilio 通话意外掉线