2011-01-28 77 views
5

我有一些关于JavaScript的问题,我需要确定一下。为了帮助,我有一个简单的类definiton我写:设计一个正确的方式

var dataSource = function (src, extension) { 
    return { 
     exists: function() { 
      // function to check if the source exists (src *should* be an object 
      // and extension should be a string in the format ".property.property.theSource". 
      // this function will return true if src.property.property.theSource exists) 
     }, 
     get: function() { 
      // function will return the source (ex: return src.property.property.theSource) 
     } 
    } 
}(); 

问题:

1)在我目前的JavaScript的理解,调用数据源()将创建自己的副本的新对象存在()和get()方法的。我对么?

2)有没有办法写这个,所以如果我创建了1,000,000个dataSource对象,我只需要每个函数都有一个副本?

3)我应该关心(2)吗?

回答

7

你在那里有一个函数返回一个istance的对象,而不是一个JS类。

你会想看看使用DataSource.prototype,你应该添加属性或构造函数内修改this如果你想在同时使用这项new

或许你也应该做这样的事情:

function DataSource(src, extension){ 
    //Make sure this behaves correctly if someone forgets to use new 
    if (! this instanceof DataSource) 
     return new DataSource(src,extension); 
    //store the constructor arguments 
    //so you can use them in the shared methods below 
    this._src=src; 
    this._extension=extension; 
} 
DataSource.prototype.exists=function(){ 
    //use this._src and this._extension here 
    //This method will be available to all 
    //objects constructed by new DataSource(...) 
}; 
DataSource.prototype.get=function(){ 
    //use this._src and this._extension here 
    //This method will be available to all 
    //objects constructed by new DataSource(...) 
}; 

var instance = new DataSource('a source','an extension'); 

编辑:你提到你希望 '私人' 变量

构建倒闭是我的经验,但是模拟的私人性质,与_前缀他们,让您的组织中的约定,以不依赖于_前缀变量的唯一便携式方式在大多数情况下足够

5

Prototype是你想要使用的。它将被存储一次并与该对象的所有实例关联。

+0

我还可以让`src`和`extension`保持私密吗? – JustcallmeDrago 2011-01-28 07:59:19

+0

@JustCallmeDrago,你一直在做什么 - 构建闭包 - 是模拟私人变量的唯一便携方式。根据我的经验,用`_`作为前缀并在你的组织中有一个不依赖`_`前缀变量的约定在大多数情况下就足够了 – tobyodavies 2011-01-28 08:07:27

+0

@Toby:啊。这就是我一直在寻找的。如果你将这个区别添加到你的答案中,我会接受它! – JustcallmeDrago 2011-01-28 08:21:24

0

你可以创建这样的类来轻松制作多个副本。

  • 编辑 - 添加了构造函数参数。

    function DataSource(src, extension) { 
        this.src = src, 
        this.extension = extension, 
        this.exists = function() { 
         // function to check if the source exists (src *should* be an object 
         // and extension should be a string in the format ".property.property.theSource". 
         // this function will return true if src.property.property.theSource exists) 
        }, 
        this.get = function() { 
         // function will return the source (ex: return src.property.property.theSource) 
        } 
    } 
    dataSource1 = new DataSource(); 
    dataSource2 = new DataSource(); 
    
0

您已经返回值插入类变量。因此,您实例化的对象数量很多,因此会创建很多实例。

根据您的要求,如果您将类变量与返回类型分开并声明一次(对于所有),那么对于每个实例,这些属性将可用。这意味着这些变量(定义为ExampleClass.prototype =函数(){})将作为在C/C++

0

更新的ES6类语法(这是写入@ tobyodavies的回答的另一种方式)一个静态变量:

class DataSource { 
    constructor(src, extension) { 
     this._src = src; 
     this._extension = extension; 
    } 
    exists() { 

    } 
    get() { 

    } 
}; 
var instance = new DataSource('a source','an extension'); 

有没有必要检查类是否与新的实例调用,因为这是不可能与ES6类。它会返回一个Class constructor DataSource cannot be invoked without 'new'错误。