Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript原型类文本最佳实践_Javascript_Prototype_Literals - Fatal编程技术网

Javascript原型类文本最佳实践

Javascript原型类文本最佳实践,javascript,prototype,literals,Javascript,Prototype,Literals,我是Javascript新手,所以我想知道下面的代码是否是一个好的实践 CustomClass = function(var1, var2) { this.var1 = var1; this.var2 = var2; }; CustomClass.prototype.aMethod = function() { console.log("my class method"); }; // Intend as main .js object, if that makes s

我是Javascript新手,所以我想知道下面的代码是否是一个好的实践

CustomClass = function(var1, var2) {
    this.var1 = var1;
    this.var2 = var2;
};
CustomClass.prototype.aMethod = function() {
    console.log("my class method");
};

// Intend as main .js object, if that makes sense
var m = {
    object1:CustomClass.prototype,
    object2:CustomClass.prototype,

    initObjects:function() {
        m.object1 = new CustomClass( value1, value2 );
        m.object1.aMethod();
        m.object2 = new CustomClass( value1, value2 );
        m.object2.aMethod();
    }
};
还是应该在“s”字面值内创建自定义类


对于您的实际代码,我们将非常感谢您的任何帮助

// globally scoped. Only avoided with closures and hoisting as shown below
// Also doesn't really make sense to define CustomClass on m unless you want
// To use it directly instead of through a factory function
CustomClass = function(var1, var2) {
    this.var1 = var1;
    this.var2 = var2;
};
CustomClass.prototype.aMethod = function() {
    console.log("my class method");
};

// Intend as main .js object, if that makes sense
var m = {
    // No reason to initialise them to the prototype. Doesn't really make sense
    // m can be edited without needing to intialise object1 or object2 at all
    object1:CustomClass.prototype,
    object2:CustomClass.prototype,

    initObjects:function() {
        m.object1 = new CustomClass( value1, value2 );
        m.object1.aMethod();
        m.object2 = new CustomClass( value1, value2 );
        m.object2.aMethod();
    }
};
一种不同的模式,可能更多地基于闭包和吊装。这采用了更具功能性的方法,并远离了标准的经典继承

// Create closure to localise scope.
(function() {
    // global object to store anything that will be hoisted to global scope
    var global = {};

    // Constructor that uses local objects and defines methods on
    // this directly.
    var CustomClass = function(_a,_b) {
         var a = _a;
         var b = _b;

         this.method = function() { console.log("foo"); }
    }

    // init function will be hoisted to global scope.
    global.init = function() {
         global.object1 = new CustomClass( v1, v2 );
         object1.method();
         global.object2 = new CustomClass( v1, v2 );
         object2.method();
    }

    // Hoist you global object into the global variable "m" on window.
    window.m = global;
}());

当然,您会丢失原型链,因此您必须使用更多的对象组合,而不是对象继承,这也会稍微慢一些。只有在创建1000多个对象的情况下,速度的损失才是真正值得注意的

恐怕你的答案中没有定义v1和v2。