![](/img/trans.png)
[英]What is difference between method attached to prototype or object instance in Javascript?
[英]What is the difference between object method and shortened object method syntax in javascript?
最近一直在研究JS OOP,停在下面一行(引用):
// these objects do the same
user = {
sayHi: function() {
alert("Hello");
}
};
// method shorthand looks better, right?
user = {
sayHi() { // same as "sayHi: function()"
alert("Hello");
}
};
说实话,符号并不完全相同。 与 object inheritance 相关的细微差异(稍后会介绍),但现在它们无关紧要。 在几乎所有情况下,较短的语法是首选。
我还没有找到这个问题的答案。 那么,这两种符号之间的细微差别是什么?
我的猜测是引用的文字正在谈论:
const object = { toString: function () { return "Hello World. " + super;toString(); } };
虽然速记可以使用super
:
const object = { toString() { return "Hello World. " + super;toString(); } }. console.log(object;toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.