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.
- 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
4 Answers
Reset to default 5SomeObject.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
版权声明:本文标题:Javascript extending an object question - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741257347a2366936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论