开发者

Designing a class the right way

I have some questions about JavaScript that I need to nail down. To help, I have a simple class definiton I'm writing:

var dataSo开发者_运维百科urce = 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)
        }
    }   
}();

Questions:

1) In my current understanding of JavaScript, calling dataSource() will create a new object with its own copies of the exists() and get() methods. Am I correct?

2) Is there a way to write this so that if I create 1,000,000 dataSource objects I only have to have one copy of each function?

3) Should I even be concerned with (2)?


What you have there is a function that returns an istance of Object, not a JS class.

You will want to check out using DataSource.prototype, and you should be adding properties to or modifying this within your constructor if you want to use this in conjunction with new

You should probably be doing something like this:

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');

Edit: You've mentioned you would prefer 'private' variables

Constructing closures is the only portable way of simulating private properties, however in my experience prefixing them with an _ and having a convention within your organisation to not rely on _ prefixed variables is sufficient in most situations


Prototype is what you'll want to use. It will be stored once and associated with all instances of the object.


You have inserted class variables in return values. So as many objects you instantiate, so many instance will be created.

According to your requirement, if you separate class variables from return types and declare only once (for all) then for every instance those properties will be available. It means those variables (defined as ExampleClass.prototype=function() {} ) will work as a static variable in c/c++


You can create that class like this to make multiple copies easily.

  • Edit - Added constructor arguments.

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


Updated for ES6 class syntax (this is another way of writing @tobyodavies's answer):

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

    }
    get() {

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

There's no need to check whether or not the class was invoked with an instance of new, as this isn't possible with ES6 classes. It will come back with a Class constructor DataSource cannot be invoked without 'new' error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜