0

I would like to create a function that can be used by every of my literal object and refer to the caller with this argument.

Something like that:

const literalObject = { 
    "value" : "string"
}

const genricFunction = () =>{
    console.log("value : ",this.value)
}

literalObject.genricFunction()
MrRock
  • 1
  • 2
  • 5
    You can add a property to the `Object.prototype` object, which is a practice that many people would say is a bad idea. That's how you'd do it though. – Pointy Mar 28 '19 at 15:31
  • See [How to make a “dot function” in javascript](https://stackoverflow.com/q/19872917/1715579) – p.s.w.g Mar 28 '19 at 15:36
  • I Have tried but ".this" return "null" – MrRock Mar 28 '19 at 15:42

4 Answers4

1

You can do something like this

function obj() {
    this.value = 'string'
}

obj.prototype.getValue = function() {
   return this.value
}

let myObj = new obj()
myObj.getValue(); // 'string' 
samb102
  • 1,104
  • 5
  • 13
  • This is the best way to do it, but you could improve this answer by adding some explanation and using the same names as the OP (`myObj` -> `literalObject` and `getValue` -> `genricFunction`) so that they can map it to their question more easily. – Paul Mar 28 '19 at 15:44
0

You can do something like this:

const obj ={genricFunction:function(){console.log("value : ",this.value)}}

And then create every object like this:

const newObj = Object.create(obj)
newObj.value=“string”
richard nelson
  • 247
  • 4
  • 12
0

You can assign your generic function to the Object.prototype. Here's your updated code.

const literalObject = { 
    "value" : "string"
}

const genericFunction = function() {
    console.log("value : ",this.value)
}

Object.prototype.genericFunction = genericFunction

literalObject.genricFunction()

Although this will work, this is considered as bad practice. What is the usecase you want this for, maybe we can suggest you a better way.

Tanmay_vijay
  • 569
  • 3
  • 12
  • It works but strange thing, when I use `() =>` instead of `function ()`, `.this` point on the Window element. – MrRock Mar 28 '19 at 16:12
  • @MrRock That's not strange, that's the main difference between arrow functions and regular functions (arrow functions don't get a `this` when they're called, so `this` just references the same this as the next lexical scope). – Paul Mar 28 '19 at 22:08
-1

You can create the literalObject and the genericFunction :

const literalObject = { 
    "value" : "string"
}

const genricFunction = function () {
    console.log("value : ",this.value)
}

and then assign a property to the literalObject with bound genericFunction:

literalObject.genericFunction = genricFunction.bind(literalObject);

and whenever you call

literalObject.genricFunction();

it will use the literalObject as this

    const literalObject = { 
        "value" : "string"
    }
    
    const genricFunction = function () {
        document.write(JSON.stringify(this.value));
    }
    
    literalObject.genricFunction = genricFunction.bind(literalObject);
    
    literalObject.genricFunction();
Teneff
  • 30,564
  • 13
  • 72
  • 103