我知道这是可行的:

function Foo() {};
Foo.prototype.talk = function () {
    alert('hello~\n');
};

var a = new Foo;
a.talk(); // 'hello~\n'

但如果我想打电话

Foo.talk() // this will not work
Foo.prototype.talk() // this works correctly

我找到了一些制作Foo的方法。谈工作,

Foo。__proto__ = Foo.prototype Foo。talk = Foo.prototype.talk

还有别的办法吗?我不知道这样做是否正确。在JavaScript代码中使用类方法还是静态方法?


当前回答

只是额外的注释。使用类ES6,当我们创建静态方法时,javacscript引擎设置的描述符属性与老式的“静态”方法略有不同

function Car() {

}

Car.brand = function() {
  console.log('Honda');
}

console.log(
  Object.getOwnPropertyDescriptors(Car)
);

它将brand()的内部属性(描述符属性)设置为

..
brand: [object Object] {
    configurable: true,
    enumerable: true,
    value: ..
    writable: true

}
..

相比

class Car2 {
   static brand() {
     console.log('Honda');
   }
}

console.log(
  Object.getOwnPropertyDescriptors(Car2)
);

将brand()的内部属性设置为

..
brand: [object Object] {
    configurable: true,
    enumerable: false,
    value:..
    writable: true
  }

..

在ES6中static method的enumerable设置为false。

这意味着你不能使用for-in循环来检查对象

for (let prop in Car) {
  console.log(prop); // brand
}

for (let prop in Car2) {
  console.log(prop); // nothing here
}

静态方法在ES6中被视为其他类的私有属性(名称,长度,构造函数),除了静态方法仍然是可写的,因此描述符writable被设置为true {writable: true}。这也意味着我们可以推翻它

Car2.brand = function() {
   console.log('Toyota');
};

console.log(
  Car2.brand() // is now changed to toyota
);

其他回答

你可以通过以下方式实现:

function Foo() {};

Foo.talk = function() { alert('I am talking.'); };

你现在可以调用"talk"函数如下:

Foo.talk();

您可以这样做,因为在JavaScript中,函数也是对象。

下面是一个很好的例子来演示Javascript如何处理静态/实例变量和方法。

function Animal(name) {
    Animal.count = Animal.count+1||1;// static variables, use function name "Animal"
    this.name = name; //instance variable, using "this"
}

Animal.showCount = function () {//static method
    alert(Animal.count)
}

Animal.prototype.showName=function(){//instance method
    alert(this.name);
}

var mouse = new Animal("Mickey");
var elephant = new Animal("Haddoop");

Animal.showCount();  // static method, count=2
mouse.showName();//instance method, alert "Mickey"
mouse.showCount();//Error!! mouse.showCount is not a function, which is different from  Java

静态方法调用是直接在类上进行的,不能在类的实例上调用。静态方法经常用于 创建效用函数

非常清晰的描述

直接取自mozilla.org

Foo需要绑定到你的类 然后当你创建一个新实例时,你可以调用myNewInstance.foo() 如果你导入你的类,你可以调用一个静态方法

在你的例子中,如果你想要Foo.talk():

function Foo() {};
// But use Foo.talk would be inefficient
Foo.talk = function () {
    alert('hello~\n');
};

Foo.talk(); // 'hello~\n'

但这是一种低效的实现方式,使用原型更好。


另一种方式,My way定义为静态类:

var Foo = new function() {
  this.talk = function () {
    alert('hello~\n');
    };
};

Foo.talk(); // 'hello~\n'

上面的静态类不需要使用原型,因为它只会被构造一次作为静态使用。

https://github.com/yidas/js-design-patterns/tree/master/class

我使用命名空间:

var Foo = {
     element: document.getElementById("id-here"),

     Talk: function(message) {
            alert("talking..." + message);
     },

     ChangeElement: function() {
            this.element.style.color = "red";
     }
};

要使用它:

Foo.Talk("Testing");

Or

Foo.ChangeElement();