简体   繁体   中英

What is the best way to define a javascript object with methods and properties? (if its possible in a native way)

I am starting to work a lot with javascript and I was wondering what would be the best way in order to work with objects without implementing any library, just using native javascript code.

What I've done so far in order to declare an "object with properties and methods" is something like this:

var myObj = {
    prop1: 'foo',
    prop2: 'bar',

    returnProp1: function() {
        var self = myObj;
        alert(self.prop1);
    },

    returnProp1: function() {
        var self = myObj;
        alert(self.prop2);
    }
}

But as far as I know, this would be a JSON Literal object, I don't think that I could call it an object with properties an methods.

Another thing I have to do, is to declare the var self = myObj; in each function, because of the "this" being overriden when I call a function of that object say, in a click binding of a jquery selector, where "this" becomes the DOM element.

So, is this a proper way to work with javascript objects? is there a way to have "this" always set to the object itself?

Any tips and suggestion are very welcomed :)

I think this is the way to go about it.

function className() {

    var self = this; 
    self.current = 0;
    //other member variables

    self.foo = function () {
    //do something
    }

    self.loader = function () {
    // do something else
    }



}

Yes, this is one of the ways you can make something like a class. Usually, you'll see it even more refined, using a constructing function (not a constructor, which is another way to specify a class):

var Class = function() {
    var privateField1 = 0;
    var privateField2 = 0;

    var privateMethod = function() {
    };

    return {
      publicProperty1: 0,
      publicProperty2: 0,
      publicMethod1: function() {
      }
    };
}

Concerning the recourrance of var self = this; : It's normal in javascript, especially when defining callbacks.

But as far as I know, this would be a JSON Literal object, I don't think that I could call it an object with properties an methods.

That is most certainly an object with properties and methods; JSON is not involved here. (JSON is a serialization format that is designed to be compatible with JavaScript object literals, but that doesn't apply here.) It's also the “right” way to create an object.

If you want to create a constructor, it looks like this:

function Something() {
    // initialization here, if you need it
}

Something.prototype.foo = function(a) {
    return a * a;
};

var obj = new Something();
console.log(obj.foo(42));

That uses the prototype of Something for each new object created using it, and is JavaScript's implementation of “classes”, if you want to think of it that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM