admin管理员组

文章数量:1287649

I have the following code:

this.myObject = {
   key1: "val1",
   key2: "val2"
}

this.aMethod = function (newObject) {
    ...

Here I want a new object (probably that inherits from this.myObject) that contains everything in this.myObject plus whatever is in newObject also, fields in newObject should override already existing fields in this.myObject

How do I do this?

This idea is that this.myObject provides some default values - but the user of the method can override these values. I'm open to criticisms of this overall "pattern" as well. Thanks.

I have the following code:

this.myObject = {
   key1: "val1",
   key2: "val2"
}

this.aMethod = function (newObject) {
    ...

Here I want a new object (probably that inherits from this.myObject) that contains everything in this.myObject plus whatever is in newObject also, fields in newObject should override already existing fields in this.myObject

How do I do this?

This idea is that this.myObject provides some default values - but the user of the method can override these values. I'm open to criticisms of this overall "pattern" as well. Thanks.

Share Improve this question edited Nov 11, 2010 at 13:19 Andrew Barber 40.2k20 gold badges97 silver badges124 bronze badges asked Nov 11, 2010 at 13:10 bbabba 15.2k11 gold badges31 silver badges26 bronze badges 11
  • In the future, instead of asking your question in "code ments", ask it in the question text - people can see the question as containing text only and close it as "not a real question". – Oded Commented Nov 11, 2010 at 13:14
  • Is this global code or function code? – Šime Vidas Commented Nov 11, 2010 at 13:22
  • the code I showed above is itself inside of a prototype method – bba Commented Nov 11, 2010 at 13:31
  • @bba I would like to see the whole pattern (the whole prototype method) – Šime Vidas Commented Nov 11, 2010 at 13:38
  • That is pretty much the entire code. Just wrap it in something like: SomeObject.prototype.Method = function() { ... }; – bba Commented Nov 11, 2010 at 13:40
 |  Show 6 more ments

4 Answers 4

Reset to default 5
SomeObject.prototype.someMethod = function() {

    this.myObject = { key1: 1, key2: 2 };

    this.aMethod = function (o) {
        var newObject = object(this.myObject);

        for (var prop in o) {
            if (o.hasOwnProperty(prop)) {
                newObject[prop] = o[prop];
            }
        }

        // Now newObject contains all properties from the passed in object
        // and also inherits all properties from myObject

    };

};

Note: I am using the object function from @Marco's answer.

Thus spoke Douglas Crockford:

function object (o) {
  function F() {}
  F.prototype = o;
  return new F();
}

There are literally dozens of ways to do that. The videos at Yahoo Theater, and the books Javascript: The Good Parts and Object Oriented Javascript explore some trade-offs. Many javascript libraries implement a simple "class-like" inheritance pattern, but it's just a small piece of the whole cake.

this should do the work:

this.aMethod = function(newObject){
  binedObject = {};
  for(key in this.myObject){
    binedObject[key] = this.myObject[key];
  }
  for(key in newObject){
    binedObject[key] = newObject[key];
  }
  return binedObject;
}

or, if you are using jquery, in one line:

return $.extend({},this.myObject,newObject);

If this is what you're looking for:

var Base = function() {
    this.foo = "bar";
};

var MyClass = new Class({ extends: Base }, function() {

    this.myMethod = function() {
        return this.foo; // bar
    }

});

Check this out: Minified 2kb minimalistic library which you can use, https://github./haroldiedema/joii/

本文标签: Javascript extending an object questionStack Overflow