Note that there are some explanatory texts on larger screens.

plurals
  1. POHow best to inherit from native JavaScript object? (Especially String)
    primarykey
    data
    text
    <p>I'm a long-time browser but a first time participator. If I'm missing any etiquette details, please just let me know!</p> <p>Also, I've searched high and low, including this site, but I haven't found a clear and succinct explanation of exactly what I'm looking to do. If I just missed it, please point me in the right direction!</p> <p>Alright, I want to extend some native JavaScript objects, such as Array and String. However, I do not want to actually extend them, but create new objects that inherit from them, then modify those.</p> <p>For Array, this works:</p> <pre><code>var myArray = function (n){ this.push(n); this.a = function (){ alert(this[0]); }; } myArray.prototype = Array.prototype; var x = new myArray("foo"); x.a(); </code></pre> <p>However, for String, the same doesn't work:</p> <pre><code>var myString = function (n){ this = n; this.a = function (){ alert(this); }; } myString.prototype = String.prototype; var x = new myString("foo"); x.a(); </code></pre> <p>I've also tried:</p> <pre><code>myString.prototype = new String(); </code></pre> <p>Now, in trying to research this, I've found that this does work:</p> <pre><code>var myString = function (n){ var s = new String(n); s.a = function (){ alert(this); }; return s; } var x = myString("foo"); x.a(); </code></pre> <p>However, this almost feels like 'cheating' to me. Like, I <em>should</em> be using the "real" inheritance model, and not this shortcut.</p> <p>So, my questions:</p> <p>1) Can you tell me what I'm doing wrong as regards inheriting from String? (Preferably with a working example...)</p> <p>2) Between the "real" inheritance example and the "shortcut" example, can you name any clear benefits or detriments to one way over the other? Or perhaps just some differences in how one would operate over the other functionally? (Because they look ultimately the same to me...)</p> <p>Thanks All!</p> <p><strong>EDIT:</strong></p> <p>Thank you to everyone who commented/answered. I think @CMS's information is the best because:</p> <p>1) He answered my String inheritance issue by pointing out that by partially redefining a String in my own string object I could make it work. (e.g. overriding toString and toValue) 2) That creating a new object that inherits from Array has limitations of its own that weren't immediately visible and can't be worked around, even by partially redefining Array.</p> <p>From the above 2 things, I conclude that JavaScript's claim of inheritablity extends only to objects you create yourself, and that when it comes to native objects the whole model breaks down. (Which is probably why 90% of the examples you find are Pet->Dog or Human->Student, and not String->SuperString). Which could be explained by @chjj's answer that these objects are really meant to be primitive values, even though everything in JS seems to be an object, and should therefore be 100% inheritable.</p> <p>If that conclusion is totally off, please correct me. And if it's accurate, then I'm sure this isn't news to anyone but myself - but thank you all again for commenting. I suppose I now have a choice to make:</p> <p>Either go forward with parasitic inheritance (my second example that I now know the name for) and try to reduce its memory-usage impact if possible, or do something like @davin, @Jeff or @chjj suggested and either psudo-redefine or totally redefine these objects for myself (which seems a waste).</p> <p>@CMS - compile your information into an answer and I'll choose it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload