JS私有方法未在每次构造函数调用时重新定义 [英] JS Private methods not redefined at each constructor call

查看:156
本文介绍了JS私有方法未在每次构造函数调用时重新定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使每次调用构造函数都未重新定义的Javascript私有方法?

How do you make a Javascript private method that is not redefined each time you call the constructor ?

据我所知,在OOP-JS中,私有方法是在一个人的类"的构造方法"中定义的方法,每次实例化一个新的对象"时都会调用该方法.我在想也许可以使用函数声明(即function name(),而不是函数表达式var name = function())来解决问题,但是如何确定以下代码只声明一次函数?

As far as I know, in OOP-JS, private methods are methods defined in the "constructor method" of one's "class", called each time one instantiates a new "object". I was thinking maybe a function declaration (i.e. function name(), as opposed to function expression var name = function()) would do the trick, but how can I be sure that the following code only declares my function once ?

​function Tester() {
    function test () {
        console.log("executed");
    }
}
var t1 = new Tester();
var t2 = new Tester();

推荐答案

如何使每次调用构造函数都未重新定义的Javascript私有方法?

How do you make a Javascript private method that is not redefined each time you call the constructor ?

您不能(嗯,请参阅下面的摆动空间).但是,除非您要拥有成千上万个Tester实例,否则不要太担心它.大多数引擎可能会在创建的多个函数对象之间重用基础代码. (请注意, code ;不是函数对象或它关闭的上下文,必须必须是唯一的并且每次都分配.但是它们不必很大.当然,相当功能函数也相当小...)

You can't (well, see below for a bit of wiggle room). But unless you're going to have thousands of instances of Tester, don't worry about it too much; most engines probably reuse the underlying code across the multiple function objects that get created. (The code, mind; not the function object or the context it closes over, which must be unique and allocated each time. But they need not be large. Of course, quite a function functions are fairly small as well...)

...如何确定以下代码仅声明一次函数?

...how can I be sure that the following code only declares my function once ?

您可以确定它不会;每次调用Tester时,它都会声明该函数.证人:

You can be sure that it doesn't; it declares the function each time Tester is called. Witness:

​function Tester() {
    this.test = test;
    function test () {
        console.log("executed");
    }
}
var t1 = new Tester();
var t2 = new Tester();
console.log(t1.test === t2.test); // "false"


请注意,您可以具有实现专用的功能,但未分配给对象的任何实例.模块模式很容易做到这一点:


Note that you can have functions that are private to the implementation, but not assigned to any instance of the object. The module pattern is handy for doing that:

var Tester = (function() {

    function Tester(name) {
        this.name = name;
    }
    Tester.prototype.publicFunction = function() {
        privateFunction.call(this);
    };

    function privateFunction() {
        console.log("My name is " + this.name);
    }

    return Tester;
})();

var t = new Tester("Fred");
t.publicFunction(); // Outputs "My name is Fred" via the private function

在那里,privateFunction是完全私有的,只能由匿名函数中的代码访问.它只有一个副本,但是您可以像使用privateFunction.call(this)调用Tester实例的方法一样调用它.

There, privateFunction is completely private, accessible only to the code within the anonymous function. And there's only one copy of it, but you can call it as though you were calling a method of a Tester instance using privateFunction.call(this).

当然,由于使用call的速度比正常调用要慢 ,因此您可以将实例作为参数传递:

Alternately, of course, since using call is slightly slower than doing a normal call, you could just pass the instance as an argument:

var Tester = (function() {

    function Tester(name) {
        this.name = name;
    }
    Tester.prototype.publicFunction = function() {
        privateFunction(this);
    };

    function privateFunction(t) {
        console.log("My name is " + t.name);
    }

    return Tester;
})();

var t = new Tester("Fred");
t.publicFunction(); // Outputs "My name is Fred" via the private function

当然,call的额外成本只有在出现问题时才是问题;除非您要在一个紧密的循环中调用数十万次,否则这无关紧要.因此,使用callthis还是传递参数将主要是一种样式选择.

Of course, the extra cost of call is only a problem if and when it's a problem; unless you're calling something hundreds of thousands of times in a tight loop, it's unlikely to matter. So whether to use call and this or pass an argument would be primarily a style choice.

这篇关于JS私有方法未在每次构造函数调用时重新定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆