So I'm trying something that seems tricky but can't get out of my mind.
I have an object like so:
var dummyObject = {
src: 'Source code',
fileExt: 'ejs',
}
And afterwards another function creates a copy of that object with a render()
function, so essentially it would be:
{
src: 'Source code',
fileExt: 'ejs',
render: function() {
...
}
}
Let's say I define this copy of dummyObject
to be one called copyObject
. Is there any way to call copyObject.render()
and have it access all of copyObject
properties without passing it as an argument?
It's important to note that I won't be defining the function in the object declaration but rather defining and then assigning it programmatically, something like:
var copyObject = _clone(dummyObject)
copyObject.render = function() {...}
Thanks!
EDIT: Just in case my question is misinterpreted (I typed it when it was late and I was tired). dummyObject in this case should be ignored, since copyObject is a fresh new copy of it without any references. So basically I'm adding the method to copyObject and I need it to access itself. Like this:
function selectProcessor (obj) {
var newObj = _clone(newObj)
newObj.render = function () {
console.log(this.src) // It doesn't make sense accessing this from here
}
return newObj
}
// At some point I execute this function
var copyObject = selectProcessor(dummyObject)
// Now I call its render function, and the behavior I want is to log its src property
copyObject.render()