我正在寻找正确的方法来添加“静态”方法(类级方法)到对象和它在JavaScript中的所有子对象。
我想做的是:
Object.prototype.myMethod = function() { ... };
// actually I need only class level method
// but it creates instance method too
我被通缉了:
function MyClass() {};
var mc = new MyClass();
mc.myMethod();
但我也变得不受欢迎:
mc.someProperty = 'val'
当我想向JavaScript中的Object类添加一个方法时,我遇到了一个问题。我不知道Object.myMethod和Object.prototype.myMethod有什么不同。
Object.myMethod = function (){};
Object.prototype.myMethod = function (){};
有人能帮我吗?
我正在使用ECMAScript6实现一个客户端应用程序,并使用进行静态代码分析。我经常在代码中使用以下模式:
class MyClass {
constructor() {
//This is how I would like to call myMethod
myMethod();
//This is how I should call myMethod to make JSHint analysis pass
this.myMethod();
}
myMethod(){
//Imp
具有以下.js文件:
var MyClass = function() {
};
MyClass.prototype = function myMethod(name) {
// my statements here...
// name is undefined here, but who calls this function and why?
return {
myMethod: myMethod
};
}();
和一页载入它的页面:
<head>
...
<script src="myfile.