cuốn sách gpt4 ai đã làm

javascript - 使函数仅对其原型(prototype)可用

In lại Tác giả: Walker 123 更新时间:2023-11-28 20:53:26 27 4
mua khóa gpt4 Nike

Đây là mã của tôi:

Random = function(name) {
this.name = tên;

this.addSomething = function(a, b) {
return a + b;
}
}

Random.prototype.addStuff = function(a, b, c, d) {
return this.addSomething(a, b) + this.addSomething(b, d);
}

我需要 this.addSomething 仅在函数本身及其原型(prototype)中可用,但这不起作用。

有人能给我指出正确的方向并告诉我如何做到这一点吗?谢谢。

câu trả lời hay nhất

创建“私有(private)”成员/方法的唯一方法是这样的:

Random = function(name) {
this.name = tên;

function add_something(a, b) {
return a + b;
}
this.addStuff = function(a, b, c, d) {
return add_something(a, b) + add_something(b, d);
}
}

biên tập:正如 RobG 在他的回答中提到的那样,this article Douglas Crockford 的著作将帮助您了解公共(public)、私有(private)和特权成员/方法在 Javascript 中的工作原理。这是一本非常值得推荐的读物。

关于javascript - 使函数仅对其原型(prototype)可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12025610/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress