How can I make a prototype function to all my object properties?

Ian Ramos

I have an object of functions and I like to prototype another function to all of these properties.

Example:

var test = function(num) {
  return num * num / (num/4);
}

var obj = {
  item1: item1, //this is a function already setted.
  item2: item2, //this is a function already setted.
  item3: item3 //this is a function already setted
}

What I want is something like: obj.item1().test() without setting this.test inside each function.

user663031

Your test function

return num * num / (num/4);

is exactly equivalent to num * 4 as far as I can see.

Anyway, the example itemX given in your comment returns a number. The only way to be able to call the function test on a number is to add it to the Number prototype, which some people would say is a bad idea:

Object.defineProperty(Number.prototype, 'test', { value: function () {
    return this * this / (this/4);
});

If you want to be able to chain test from the result of the call to obj.itemX, then the latter needs to return this, not this.num:

var itemX = function(val) {this.num = val; return this};

Now you just need to place a modified version of test once on obj:

var obj = {
  item1: item1, //this is a function already set.
  item2: item2, //this is a function already set.
  item3: item3, //this is a function already set.

  test: function() { return test(this.num); }
};

> obj.item1(22).test()
< 88

How can I make a prototype function to all my object properties?

If you are referring to test, this is not a prototype function. It's just a plain old method on an object. To add it, just add it on the object, as done above.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I call a function only after all properties have been set in my UIView subclass?

From Dev

How can I call a function only after all properties have been set in my UIView subclass?

From Dev

how can I make my numerous button activated functions into one function that handles all numbers?

From Dev

Set prototype for all object properties

From Dev

Set prototype for all object properties

From Dev

How can I find all properties of a COM object?

From Dev

How can I make a JSON Object containing two properties from a JSON Object containing four properties

From Dev

How can I make a list of all dataframes that are in my global environment?

From Dev

How can I make all my processes start with niceness 5

From Dev

How can i make my jQuery code work on all elements

From Dev

How can I make my form responsive on all sizes

From Dev

How can I have prototype function as parameters?

From Dev

Rails: How can I make an object available in all views?

From Dev

How can I make an object once for all methods?

From Dev

How can I make my function run in constant time?

From Dev

How can I make my Rust function more generic & efficient?

From Dev

How can I make my function to run the moment the page is running?

From Dev

How can I make the LLVM IR of a function available to my program?

From Dev

How can I make my IsItAHoliday function more efficient?

From Dev

how can i make my function inverse in r?

From Dev

How can I make the LLVM IR of a function available to my program?

From Dev

How can I make my function accept different types of input?

From Dev

How can I make certain properties of my EF code-first classes internal to my DAL?

From Dev

How can I make certain properties of my EF code-first classes internal to my DAL?

From Dev

How can I pass all arguments to my callback function?

From Dev

How can I make an object property be both a function and a variable in Javascript?

From Dev

how I can move my object with the MouseMove function?

From Dev

How do I make my method generic whereby I can pass in properties?

From Dev

Object Prototype: Case Insensitive Getter On All Properties

Related Related

  1. 1

    How can I call a function only after all properties have been set in my UIView subclass?

  2. 2

    How can I call a function only after all properties have been set in my UIView subclass?

  3. 3

    how can I make my numerous button activated functions into one function that handles all numbers?

  4. 4

    Set prototype for all object properties

  5. 5

    Set prototype for all object properties

  6. 6

    How can I find all properties of a COM object?

  7. 7

    How can I make a JSON Object containing two properties from a JSON Object containing four properties

  8. 8

    How can I make a list of all dataframes that are in my global environment?

  9. 9

    How can I make all my processes start with niceness 5

  10. 10

    How can i make my jQuery code work on all elements

  11. 11

    How can I make my form responsive on all sizes

  12. 12

    How can I have prototype function as parameters?

  13. 13

    Rails: How can I make an object available in all views?

  14. 14

    How can I make an object once for all methods?

  15. 15

    How can I make my function run in constant time?

  16. 16

    How can I make my Rust function more generic & efficient?

  17. 17

    How can I make my function to run the moment the page is running?

  18. 18

    How can I make the LLVM IR of a function available to my program?

  19. 19

    How can I make my IsItAHoliday function more efficient?

  20. 20

    how can i make my function inverse in r?

  21. 21

    How can I make the LLVM IR of a function available to my program?

  22. 22

    How can I make my function accept different types of input?

  23. 23

    How can I make certain properties of my EF code-first classes internal to my DAL?

  24. 24

    How can I make certain properties of my EF code-first classes internal to my DAL?

  25. 25

    How can I pass all arguments to my callback function?

  26. 26

    How can I make an object property be both a function and a variable in Javascript?

  27. 27

    how I can move my object with the MouseMove function?

  28. 28

    How do I make my method generic whereby I can pass in properties?

  29. 29

    Object Prototype: Case Insensitive Getter On All Properties

HotTag

Archive