Basicly I'm trying to add an object with my own functions inside the object.prototype.
Object.prototype.personalTest = {
sum: function () {
return this.something + this.other;
}
};
var numbers = {
something: 1,
other: 2
};
console.log(numbers.personalTest.sum());
Problem is I can't get the value from the original object. The 'this' keyword uses my object as the 'this'.
How can I change the value of the 'this' or pass it to the object?
Edit I did this and it kind of worked but not as I wanted
var personalAccess = function () {
var self = this;
this.PersonalTools = {
sum: function () {
return self.something + self.other;
},
minus: function () {
return self.something - self.other;
}
};
};
Object.prototype.personalTest = personalAccess;
var numbers = {
something: 1,
other: 2
};
console.log(numbers.personalTest());
The objects aren't part of the prototype anymore but that's not a problem. The problem is that for each variable i have to build the objects using the function.
console.log(numbers.personalTest());
..........Solution...........
I ended up learning a bit more tricks on javascript and used factory functions to solve my issue.
(function () {
var myTools = function () {
var self = this;
var tools = {
sum: self.first + self.second
};
return tools;
};
Object.prototype.MyTools = myTools;
}());