我需要使用新语法向Javascript类添加方法。我这样尝试:
class X{
constructor() {
this.a = 'b'
}
x(){
}
}
X.prototype.y = function (){
console.log('y')
}
var x = new X()
x.y()
console.log(X) // print the the class but not the new method.
它只是打印:
class X{
constructor() {
this.a = 'b'
}
x(){}
}
但是我期望
class X{
constructor() {
this.a = 'b'
}
x(){}
y(){
console.log('y');
}
}
如何向Javascript类添加新方法?
这项工作正常,如果您正在google chrome控制台中进行检查,请通过扩展原型节点进行检查。或者尝试检查console.log(X.y)
,console.log(X.prototype.y)
否则console.log(x.y)
必须打印该功能
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句