var MyClass = function() {};
MyClass.method = function (name, age) {
return "My name is " + name + " and I'm " + age + " years old";
}
MyClass.prototype.method = function (name, age) {
return "My name is " + name + " and I'm " + age + " years old";
}
console.log(MyClass.method('David','30'));
var instance = new MyClass();
console.log(instance.method('john', '23'));
在上面的代码片段中,两种方法都在做同样的事情。在大多数情况下,两者都可以互换使用。你会选择哪一个,为什么?