gpt4 book ai didi

javascript - javascript 中的类和 'access modifiers'

转载 作者:数据小太阳 更新时间:2023-10-29 06:10:21 24 4
gpt4 key购买 nike

我试图了解“类”在 ES5 中是如何工作的,以及我如何将我对传统的、类型化的面向对象语言(如 Java)的知识应用到 javascript 中。

在下面的代码示例中,我评论了我的问题。

var MyClass = (function () {

// [What am I?] A private variable?
var myVariable1

// Constructor.
function MyClass() {
// Essentially a public variable.
this.myVariable2 = 0;
}

// Public method returning myVariable1.
MyClass.prototype.myMethod1 = function () {
return myVariable1;
};

// Public method returning public variable.
MyClass.prototype.myMethod2 = function () {
return this.myVariable2;
};

// [What am I?] A private method?
function myMethod3 () {
return 0;
}

return MyClass;

}());

我主要想知道“私有(private)”的东西。比如,如果我有这个类的多个实例会发生什么?它们可以相互干扰私有(private)变量和函数吗?

最佳答案

首先,我将解决这一点:

...and how I can apply my knowledge of traditional, typed object oriented languages like Java to javascript.



你可以经常认为 JavaScript 有点像 Java/C#/等,但如果你这样做,A) 它会在某些时候咬你,因为 JavaScript 是 不是 就像 Java/C#/etc.,它是根本不同的,尽管它有一些使它看起来相似的陷阱;和 B) 如果不知道如何使用 JavaScript 根本不同的性质,您将错过 JavaScript 的真正力量。 (我怎么知道?我正是这样做的,来自 Java [甚至 C++] 背景并假设 JavaScript 是相似的。)

这绝不是任何形式的批评,这只是智者的一句话。尝试深入学习 JavaScript 原型(prototype)继承和闭包是如何工作的,你会发现你解决问题的方式与你在 Java/C#/etc 中的方式略有不同。

内联答案:
var MyClass = (function () {

// [What am I?] A private variable?
// ==> A private *class* variable (static variable). Not instance-specific.
var myVariable1

// Constructor.
function MyClass() {
// Essentially a public variable.
// ==> A public field/property/variable, choose your terminology. :-)
// ==> The Java spec calls them both fields and variables (oddly).
// ==> In JavaScript, they're called properties.
this.myVariable2 = 0;
}

// Public method returning myVariable1.
// ==> Correct, but note that myVariable1 is shared across all instances
MyClass.prototype.myMethod1 = function () {
return myVariable1;
};

// Public method returning public variable.
// ==> Yes, note that this one is returning an instance-specific field/property/variable
MyClass.prototype.myMethod2 = function () {
return this.myVariable2;
};

// [What am I?] A private method?
// ==> A private *class* method (static method), yes.
function myMethod3 () {
return 0;
}

return MyClass;

}());

I am mostly wondering about the "private" stuff. Like, what happens if I have multiple instances of this class? Can they interfere with each others private variables and functions?



是的,正如我在评论中提到的,它们是全类(class)的。如果你想拥有一个私有(private)的“实例字段”,你必须在你的构造函数中声明它,并在构造函数中创建任何需要访问它的函数,以便它们关闭它:
function MyClass(arg) {
var privateInstanceInfo = arg; // We could also just use arg directly

this.accessThePrivateInstanceInfo = function() {
return privateInstanceInfo * 42;
};
}
MyClass.prototype.useOnlyPublicInfo = function() {
// This cannot access `privateInstanceInfo` directly.
// It *can* access the function `accessThePrivateInstanceInfo`
// because that's public:
return accessThePrivateInstanceInfo();
};
var c = new MyClass(2);
console.log(c.useOnlyPublicInfo()); // 84

这完全取决于一个称为闭包的概念,在这个问题及其答案中有详细概述: How do JavaScript closures work?我也会引用我几年前的文章 Closures are not complicated ,虽然与最新规范相比,它使用了一些较旧的术语,但仍然很好地描述了这些概念。

您用作包装器的内联调用函数 (IIFE) 创建了一个单独的执行上下文,其中的所有函数都关闭了该上下文。这意味着他们可以实时访问在该上下文中定义的变量和函数,即使在函数返回之后也是如此。因为你只调用一次,所以只有一个上下文,所以只有一个 myVariable1myMethod3 .

在我创建私有(private)实例信息的示例中,我们使用为每次调用构造函数创建一个新上下文的事实。该上下文不与其他任何内容共享,因此成为特定于实例的。

有一种方法可以获取近乎私有(private)的实例属性,而无需在构造函数中定义函数,即使用随机选择的名称:
function makeRandomName() {
var str = "";
while (str.length < 10) {
str += String.fromCharCode(32 + Math.floor(95 * Math.random()));
}
return "__" + str;
}
var MyClass = (function() {
var pseudoPrivatePropName = makeRandomName();

function MyClass() {
this[pseudoPrivatePropName] = 42;
}

// ....

return MyClass;
})();

IIFE 内部的代码知道属性的名称,而其外部的代码则不知道。现在,恶意代码仍然可以找到该属性,但它变得更具挑战性,尤其是当您拥有多个属性时。 (如果你有多个,你需要一个比上面更好的名称分配器。)

在 ES2015 (ES6) 中,您可以使用
var pseudoPrivatePropName = Symbol();

...而不是拥有名称分配器。

详情在 this somewhat-outdated article在我的博客中。

关于javascript - javascript 中的类和 'access modifiers',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38243329/

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