gpt4 book ai didi

javascript - 写JS Prototypes,所有的函数都应该使用Prototype对象吗?

转载 作者:行者123 更新时间:2023-12-02 04:57:16 24 4
gpt4 key购买 nike

我开始学习更多关于使用 Prototype 对象编写 JS 的知识,但我想确保我不会从其他开发人员那里吸取任何坏习惯。我对使用 Prototype 的理解是为您的实例创建公共(public)方法。例如:

var module = new Module();
module.method();

但我看到很多开发人员在 Prototype 对象中创建他们的所有代码,我认为这些东西是“私有(private)的”。这是不好的做法还是可以接受?这只是意味着我可以这样做:

module.privateFn();

他们知道吗?这样可以吗?任何帮助表示赞赏。我一直在查看 GitHub 上的源代码以尝试建立最佳前进方式,这是一个使用原型(prototype)的脚本(例如他们显然希望私下保存的 attachEvent):

https://github.com/WickyNilliams/headroom.js/blob/master/dist/headroom.js

非常感谢,我想确保我使用正确的实现进行开发。

最佳答案

首先你不需要使用原型(prototype)来编写模块。想想如果你写的东西像一个类,你应该使用原型(prototype)。在何处定义您的方法也很重要。在原型(prototype)对象上定义方法和在构造函数中定义它们是完全不同的事情!

让我们看一个使用构造函数中定义的方法的示例类定义:

var Dog = (function () {
var Dog = function (age, name) {
var that = this;

this.age = age;
this.name = name;

this.sayHi = function () {
console.log('Warf! Im ' + that.name); // meaning of "this" changed!!!
};

this.anotherMethod = function () {};
};

return Dog;
}());

var puppy = new Dog(1, 'puppy'); // sayHi and anotherMethod created
var sirius = new Dog(1, 'sirius'); // sayHi and anotherMethod recreated

sirius.sayHi = function () { console.log('Yohalolop!'); };

puppy.sayHi(); // -> 'Warf! Im puppy'
sirius.sayHi(); // -> 'Yohalolop!'

所以上面的例子有一些问题,首先方法像任何其他实例变量一样被定义。实际上,是的,您将它们定义为实例变量,这意味着将为您创建的每个实例对象重新创建此函数。我想您已经提到您不能在方法定义中使用 this 关键字。这很容易出错,并且有可能忘记它并错误地使用 this 关键字。有时您可以将方法用作实例变量,当然就像变量回调一样。

让我们看一个带有原型(prototype)对象的示例类定义:

var Dog = (function () {
var Dog = function (age, name) {
this.age = age;
this.name = name;
};

// sayHi method defined only once in prototype
Dog.prototype.sayHi = function () {
console.log('Warf! Im ' + this.name; // we can use this keyword
};

// anotherMethod defined only once in protoype
Dog.prototype.anotherMethod() {
};

return Dog;
}());

var puppy = new Dog(1, 'puppy');
var sirius = new Dog(1, 'sirius'); // sirius and puppy sharing same prototype object

puppy.sayHi(); // -> 'Warf! Im puppy'
sirius.sayHi(); // -> 'Warf! Im sirius'

// remember puppy and sirius sharing same prototype object
Dog.prototype.sayHi = function () {
console.log('Yohalolop');
};

puppy.sayHi(); // -> 'Yohalolop'
sirius.sayHi(); // -> 'Yohalolop'

作为您关于私有(private)函数问题的答案,它更加复杂。是的,即使你在原型(prototype)上定义你的方法,你也可以使用私有(private)函数,但是有一些关于测试的问题。它们的使用取决于您。我宁愿不使用。让我举一些例子。

var Calculator = (function () {
var Calculator = function () {
this.importantNumber = 2;
};

// There is unfortunately no native implementation
// for private methods but you can mimic them with
// unaccessible functions and binding.
var someExtremeComputations = function () {
return 40 + this.importantNumber; // this keyword points to instance because of binding
};

Calculator.prototype.getMeaningOfLife = function () {
var result = someExtremeComputations.call(this); // we bind function to instance
return result;
};

return Calculator;
}());

这是您如何在 javascript 中定义私有(private)方法的示例之一。私有(private)函数的问题,无法测试。无法测试 someExtremeComputations 方法。

有些人(包括我)对私有(private)方法使用带前缀的下划线命名约定。所以它们实际上是公共(public)方法,但如果有人调用它们或覆盖它们,它们会被前缀下划线警告。毕竟我们可以测试私有(private)方法,因为它们实际上是公开的。

var Calculator = (function () {
var Calculator = function () {
this.importantNumber = 2;
};

// private method's name prefixed by an underscore to warn
// other developers to be careful about that or not to use.
Calculator.prototype._someExtremeComputations = function () {
return 40 + this.importantNumber;
};

Calculator.prototype.getMeaningOfLife = function () {
var result = this.someExtremeComputations(); // no need to bind
return result;
};

return Calculator;
}());

关于javascript - 写JS Prototypes,所有的函数都应该使用Prototype对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20471653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com