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;
}());
3

3 Answers 3

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; }

Sign up to request clarification or add additional context in comments.

3 Comments

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
@AnthonyWhite got it, well the alternative is bind those attributes to the function sum. :)
I'm going to try that. Thank you
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().

5 Comments

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?
@AnthonyWhite can you check my answer. An alternative is using setters and getters.
You can, as well, add sum() directly as a property of Object.prototype and invoke it as numbers.sum().
@AnthonyWhite that last comment from axiac is a good alternative.
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
0
numbers.personalTest.sum.call(numbers)

3 Comments

Please add somes explanations editing your answer, avoid code only answer
The call() method calls a function with a given this value and arguments provided individually.
Make a readable answer for people stackoverflow.com/help/how-to-answer not for me ;) that's why I precised editing the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.