0

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;
}());
  • Try to check out this https://stackoverflow.com/questions/2236747/use-of-the-javascript-bind-method – julian salas Mar 16 '18 at 12:16
  • Possible duplicate of [Acess to this from subobject in JavaScript](https://stackoverflow.com/questions/37527509/acess-to-this-from-subobject-in-javascript) – melpomene Mar 16 '18 at 12:19

3 Answers3

1

The main problem is that you're thinking that the function sum declared within personalTest will have access to any attributes outside from it. The scope of function sum is the object personalTest.

An alternative, is either binding that object numbers to the function sum or executing the function call passing the object numbers.

numbers.personalTest.sum.bind(numbers)();
numbers.personalTest.sum.call(numbers);

Object.prototype.personalTest = {
  sum: function () {  
    return this.something + this.other;
  }
};

var numbers = {
  something: 1,
  other: 2
};


console.log(numbers.personalTest.sum.bind(numbers)());
console.log(numbers.personalTest.sum.call(numbers));

Or, you can assign those values to personalTest to make them accesible from the function sum.

Object.prototype.personalTest = {
  sum: function () {  
    return this.something + this.other;
  }
};

var numbers = {
  something: 1,
  other: 2
};

Object.assign(numbers.personalTest, numbers);
console.log(numbers.personalTest.sum());

Another alternative, is creating setters and getters to automatically set the necessary attributes to personalTest:

Object.prototype.personalTest = {
  sum: function () {  
    return this.something + this.other;
  }
};

var numbers = {
  set something(s) {
    this.thing = this.personalTest.something = s;
  },
  get something() {
    return this.thing;
  },
  set other(s) {
    this.o = this.personalTest.other = s;
  },
  get other() {
    return this.o;
  }
};

numbers.something = 1
numbers.other = 2

console.log(numbers.personalTest.sum());
.as-console-wrapper { max-height: 100% !important; top: 0; }
Ele
  • 33,468
  • 7
  • 37
  • 75
  • That is way to complicated for me hahah. Thing is I want something like a personal "submenu" of my own functions. That's why I'm looking for something like: variable.myPersonalFunctions.function1 – Anthony White Mar 16 '18 at 13:11
  • @AnthonyWhite got it, well the alternative is bind those attributes to the function `sum`. `:)` – Ele Mar 16 '18 at 13:14
  • I'm going to try that. Thank you – Anthony White Mar 16 '18 at 13:39
0
numbers.personalTest.sum.call(numbers)
jurgen
  • 79
  • 1
  • 2
  • 3
    Please add somes explanations editing your answer, avoid code only answer – GGO Mar 16 '18 at 12:37
  • The call() method calls a function with a given this value and arguments provided individually. – jurgen Mar 16 '18 at 17:19
  • Make a readable answer for people https://stackoverflow.com/help/how-to-answer not for me ;) that's why I precised editing the answer – GGO Mar 19 '18 at 08:40
0

The code:

Object.prototype.personalTest = {
  sum: function () {
    return this.something + this.other;
  }
};

adds a new property named personalTest to each object you create.

This property is an object itself, having one property, sum, that is a function. Inside the sum() function, this refers to this object whose property sum() is (i.e. the object you create and store in Object.prototype.personalTest).

You can let sum access the properties of numbers by invoking it this way:

var numbers = {
  something: 1,
  other: 2
};

console.log(numbers.personalTest.sum.call(numbers));

This way, the function sum() (accessible only through the property personalTest.sum of any object) is invoked using numbers as this.

Read more about Function.call().

axiac
  • 68,258
  • 9
  • 99
  • 134
  • That's exactly what I needed. Thank you. But is there a way that i can use the call function without needing to write it every time? I want to keep just numbers.personalTest.sum() Is it possible? – Anthony White Mar 16 '18 at 12:33
  • @AnthonyWhite can you check my answer. An alternative is using setters and getters. – Ele Mar 16 '18 at 12:55
  • You can, as well, add `sum()` directly as a property of `Object.prototype` and invoke it as `numbers.sum()`. – axiac Mar 16 '18 at 12:58
  • @AnthonyWhite that last comment from axiac is a good alternative. – Ele Mar 16 '18 at 13:02
  • Thing is I want something like a personal "submenu" of my own functions. That's why I'm looking for something like: variable.myPersonalFunctions.function1 – Anthony White Mar 16 '18 at 13:11