Javascript 原型类文字最佳实践

发布于 2024-10-22 05:30:58 字数 623 浏览 2 评论 0原文

我是 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”文字中创建自定义类?

任何帮助将不胜感激

I am new to Javascript, so I wonder if the following code is a good practice.

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();
    }
};

Or should I create my custom class inside the "s" literal?

Any help will be much appreciated

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

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

发布评论

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

评论(1

寒冷纷飞旳雪 2024-10-29 05:30:58

至于您的实际代码:

// 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 多个对象时,速度损失才会真正明显

As to your actual code:

// 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();
    }
};

A different pattern that might be of use based more on closures and hoisting. This takes a more functional approach and moves away from standard classical inheritance.

// 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;
}());

Of course you lose the prototype chain so your must use a lot more object composition instead of object inheritance and this is also slower by a small amount. The loss of speed is only really noticable if your creating 1000+ objects

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