Difference between methods of defining JavaScript 'class

Difference between methods of defining JavaScript 'classes'

本文关键字:class JavaScript defining between methods of Difference      更新时间:2023-09-26

这两种在JavaScript中定义"class"的方法有什么区别?

方法一

在构造函数中定义方法:

function MyClass()
{
    this.foo = function() { console.log('hello world!'); };
}

方法二

在原型上定义方法:

function MyClass()
{}
MyClass.prototype.foo = function() { console.log('hello world!'); };

第一个将在对象的每个实例化上创建一个新的函数对象,第二个将为每个实例分配一个对原型方法的引用。简而言之:第二个更有效,因为所有实例都将共享一个函数对象。

这只是原型链的逻辑,您可以尝试并通过任何对象访问任何内容:

var objLiteral = {foo:'bar'};

访问objLiteral.foo时,JS将首先查看对象本身定义的属性,如果找到该值,则返回该值。如果JS找不到对象本身的属性,它将检查对象的原型,因此:

objLiteral.valueOf();//method defined @Object.prototype
objLiteral.valueOf === Object.prototype.valueOf //true

但当你使用第一种方法时:

function SomeConstructor()
{
    this.methd = function()
    {
        return true;
    }
}
var f = new SomeConstructor();
var g = new SomeConstructor();
f.methd === g.methd;//FALSE!

这表明我们正在处理两个独立的函数对象。将函数定义移动到原型,f.methd === g.methd;将为真:

function SomeConstructor()
{
}
SomeConstructor.prototype.methd = function()
{
    return true;
}
var f = new SomeConstructor();
var g = new SomeConstructor();
f.methd === g.methd;//true!

回复您的评论:

在原型级别上定义方法允许您更改特定任务的方法,然后将其"重置"为默认行为。假设您所在的函数正在创建AJAX请求:

someObject.toString = function(){ return JSON.stringify(this);}
//when concatinating this object you'll get its json string
//do a lot of stuff
delete (someObject.toString);

JS将再次检查对象是否在其自身上定义了toString属性。因此JS将删除您分配给toString属性的函数。下次调用toString时,JS将重新开始扫描原型链,并使用该方法(在原型中)的第一次出现。让我们澄清一下:

function SomeConstructor()
{
}
SomeConstructor.prototype.methd = function()
{
    return true;
}
var f = new SomeConstructor();
var g = new SomeConstructor();
f.methd = function(){return false;};
g.methd();//returns true, still <-- method is gotten from the prototype
f.methd();//returns false <-- method is defined @ instance level
delete (f.methd);
f.methd();//returns true, f doesn't have the method, but the prototype still does, so JS uses that.

或者更好的是,您甚至可以用另一个原型的方法替换实例的方法:

f.methd = Object.prototype.valueOf;//for as long as you need

最后一个例子毫无意义,因为f已经有了valueOf方法:它的继承链看起来像这样:var f ---> SomeConstructor ---> Object,也允许您访问所有Object.prototype方法!整洁,不是吗?

这些只是虚设的例子,但我希望你能看到,这是使JS成为一种极其灵活(有时我必须承认,过于灵活)和富有表现力的语言的特性之一。

在第一种情况下,将为每个实例创建函数,并将其设置为对象中的foo属性。在第二种情况下,它是共享函数。当你调用obj.prop时,它会在对象本身中查找它,如果它不在那里,那么它会在proto对象中查找,依此类推,它被称为chain of prototypes

例如,此代码提供foo:

function MyClass() {
  this.foo = function () {};
}
var myVariable = new MyClass();
for (var i in myVariable) if (myVariable.hasOwnProperty(i)) console.log(i);

但这不是:

function MyClass() {
}
MyClass.prototype.foo = function () {};
var myVariable = new MyClass();
for (var i in myVariable) if (myVariable.hasOwnProperty(i)) console.log(i);