9
I am implementing a method extension library of prototype
, and if I do it simply everything works perfectly as you can see in the simple example below:
String.prototype.append = function(value) {
// aqui o this é a instancia da String. Ok!
return this.toString() + value;
};
document.getElementById('result').textContent = "Concatena com ".append("isto!");
<p id="result">
</p>
But to avoid overwriting methods of prototype
, created an object within the prototype
to register these methods, but with this the scope of the method is modified and the this
is no longer a reference to the String
, as can be seen in the following example:
String.prototype.extencion = {
append: function(value) {
// aqui o this não é mas uma instancia da String. Fail =(!
// Como pegar a instancia do objecto pai?
return this.toString() + value;
}
};
document.getElementById('result').textContent = "Concatena com ".extencion.append("isto!");
<p id="result"></p>
Question
It is possible to recover the instance of the parent object in a function
in the child object?
bind will function for all major browser ? ie, Chrome, edge, opera, safari, Godzilla ? In some cases you would have to create at the beginning of the script not? see here
– EProgrammerNotFound
@Eprogrammernotfound Chrome, FF, Opera, Safari and IE9+ at least (there is a polyfill those who can’t stand). What is "edge" and "Godzilla"?
– mgibsonbr
Edge is the new microsoft browser (Apparently you haven’t installed Windows 10 yet). Godzila is the mozzila firefox
– EProgrammerNotFound
@Fernando I just wanted to draw attention to the name of the function: The correct would be Extension if it is in English
– EProgrammerNotFound
That last issue killed my answer :P now I’ll have to invent another approach :)
+1
– Sergio
@mgibsonbr, are good ways to do +1! But the first way with closures as you mentioned will create a new instance every call, which is not good, the second the call becomes unintuitive, even more for an extension library! I’ll review my problem!
– Fernando Leal
@mgibsonbr, about the
innerText
fortextContent
, I changed in the question also, so it is consistent with the answer and it works in the FF, had not repaired this!– Fernando Leal
@Fernando hehe you can try to use creativity, like create a cache (especially now that the main ones browsers started to support the
WeakMap
) and only create an instance if it doesn’t exist yet, etc. Whether it compensates or not, I don’t know, I’ve gotten used to the "jQuery way" rsrs.– mgibsonbr
@Eprogrammernotfound, it was a typo, I will correct in my project! But in the question I will leave even so as not to cause inconsistency with the answer! Thank you!
– Fernando Leal
@Fernando Recreating the object is irrelevant in most cases. Don’t worry about optimizations unless you have a bottleneck in that part. I would make the most simple and readable first. As far as I know, concatenating the string also creates a new urge, no ?
– EProgrammerNotFound
@Eprogrammernotfound In theory I agree with you (in particular if the compiler is smart, and the garbage collector is generational, the overhead of the creation of the object will be minimal), in practice, only testing... :)
– mgibsonbr
I think in ES6 I could use one Proxy, and intercept the method search on the object at the time of the call.
– bfavaretto
@bfavaretto Indeed, but from what I understand this solution would suffer from the same problems as my first example (besides the obvious inconvenience of having to create the Proxy). Unless I’ve misunderstood, of course, you don’t want to post an answer with your suggestion?
– mgibsonbr
I need to research to see how it does, if it works out I post later. I’m also not sure I would suffer from the same problems you mentioned.
– bfavaretto
@bfavaretto, interesting your suggestion, despite being "futuristic" =D, since so far only Firefox and Edge have implemented this. I couldn’t quite understand how this was going to be implemented with Proxy, but it seems something at least interesting. If you can implement a simple example of your idea it would be aggregator. (I was curious =D)
– Fernando Leal
@mgibsonbr, I ended up implementing its solution number 1 (despite its problems, hehe), so I will accept this as an answer, at least until I have a better solution, maybe the Proxy that the bfavaretto quoted, but possibly will take to be compatible and usual, since there is not even a Polyfill for him. hehe
– Fernando Leal