JavaScript 最终方法

发布于 2024-11-03 15:37:18 字数 45 浏览 0 评论 0原文

javascript中的方法可以是final的吗? 如何避免它被子类覆盖?

Can a method in javascript be final?
How to avoid it to be overriden by a subclass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜吻♂芭芘 2024-11-10 15:37:18

从传统意义上讲,不,您不能拥有私有/受保护的方法或阻止它们被覆盖。

然而,您可以做的是将方法封装在一个作用域中,然后简单地不公开它们:

function foo(){
    function bar(){
        // private function
    }

    this.doSomething = function(){
        bar();
    }
}

这就是您所能得到的最接近的结果。我不久前写了一篇关于此的文章: http://www.htmlgoodies.com/primers/jsp/article.php/3600451/Javascript-Basics-Part-8.htm

您还可以使用 __defineGetter____defineSetter__ 来阻止访问,但这些并不是 100% 跨浏览器。

var x = {};
x.__defineGetter__('foo', function(){ return 10; });
x.__defineSetter__('foo', function(){});

x.foo = 'bar';
x.foo; // 10

In the traditional sense, no, you can't have private/protected methods or prevent them from being overridden.

What you can do, however, is encapsulate methods in a scope and then simply not expose them:

function foo(){
    function bar(){
        // private function
    }

    this.doSomething = function(){
        bar();
    }
}

That's about as close as you can get. I wrote an article on this a while ago: http://www.htmlgoodies.com/primers/jsp/article.php/3600451/Javascript-Basics-Part-8.htm

You can also use __defineGetter__ and __defineSetter__ to prevent access, but those aren't 100% cross-browser.

var x = {};
x.__defineGetter__('foo', function(){ return 10; });
x.__defineSetter__('foo', function(){});

x.foo = 'bar';
x.foo; // 10
趁年轻赶紧闹 2024-11-10 15:37:18

您可以将 defineProperty 用于成员 您想要确保安全。

Mike 提到了可配置属性,用于防止重新定义 - 删除。我也添加这个。

function final(obj, members) {
    for (var i = 0; i < members.length; i++) {
        var m = members[i];
        if (obj.hasOwnProperty(m)) {
            Object.defineProperty(obj, m, { 
                value: obj[m],
                writable: false,
                configurable: false 
            });
        }
    }
    return obj;
}
var obj = { foo: "bar" };
obj = final(obj, ["foo"]);

obj.foo = ""; // output will be empty but
obj.foo // now output's still bar.

另一个选项

Object.freeze。但它保护对象中的所有成员。

var obj = { foo: "bar" };
obj = Object.freeze(obj);

obj.foo = ""; // output is empty
obj.foo // output is still "bar".

You can use defineProperty for what member you want to make secure.

Mike has mentioned configurable attribute for preventing from redefine -- delete. I'm adding this too.

function final(obj, members) {
    for (var i = 0; i < members.length; i++) {
        var m = members[i];
        if (obj.hasOwnProperty(m)) {
            Object.defineProperty(obj, m, { 
                value: obj[m],
                writable: false,
                configurable: false 
            });
        }
    }
    return obj;
}
var obj = { foo: "bar" };
obj = final(obj, ["foo"]);

obj.foo = ""; // output will be empty but
obj.foo // now output's still bar.

Another option

Object.freeze. But it's secures all members in object.

var obj = { foo: "bar" };
obj = Object.freeze(obj);

obj.foo = ""; // output is empty
obj.foo // output is still "bar".
尾戒 2024-11-10 15:37:18

据我所知,该语言没有办法强制对象属性的最终确定性。你必须强制执行这一点。 “非常确定”的一种方法是使用极不可能在子类中使用的命名约定。像这样的东西

someobject.__property__

可能会工作得很好。另一种选择是使用 hasOwnProperty() 函数来检测父对象是否已经具有属性,然后再用其他内容覆盖它。

As far as I know, the language does not have a way of enforcing finality of object properties. You have to enforce this on your end. One way to be "pretty sure" is to use a naming convention that is extremely unlikely to be used in a subclass. something like

someobject.__property__

Would probably work pretty well. Another option is to use the hasOwnProperty() function to detect if a parent object already has a property before overwriting it with something else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文