JavaScript is from this

String.prototype.foo = {}; String.prototype.foo.bar = function() { //How can you reference the "grandparent" string? console.log(this.parent.parent); //obviously, doesn't exist } 

Like in, "Hello, Nurse!".foo.bar() will record โ€œHello, Nurse!โ€.

Will it matter if there is control over foo?

Edit: foo is defined there.

Edit2: Fine, instead of this.this.this , this.parent.parent . Of course, the parent language does not exist, but, hopefully, now the semantics will not interfere.

Edit3: There is no specific case. The details provided are pretty much all I have: there is a foo object, part of the prototype. foo.bar is the foo method, and it must access its progenitor. It. Nothing more. That's all I have.

Edit4: Solved. Based on the answer provided (and some second-hand help from Douglas Crockford):

 String.prototype.foo = function() { var that = this; return { bar : function() { console.log(that.valueOf()); } } } //Called: "Hello, Nurse!".foo().bar(); 
+7
source share
2 answers

The only way to do this is to turn foo() into a function. Think of it as initializing the foo namespace for a particular string:

 String.prototype.foo = function () { var str = String(this); var o = Object(this) o.bar = function () { console.log(str); }; return o; }; 

Then you can use:

 "foobar".foo().bar(); // logs "foobar" 

Or, if we rename foo and bar to something more exciting:

 "Hello!".console().log(); // logs "Hello!" 

Why is it so hard?

Each function is called with a specific context , which is the only object. Whether this is called using ab() or abcd() does not matter - the object immediately on the left of the function call is provided as its context. So the context for ab() is a , and the context for abcd() is c . This keyword refers to context. Since c is just an object (not a working function), it has no context and no concept of this , so this.this doesn't make sense.

Therefore, it is not possible to access the so-called "parent". Juan's answer provides a good conceptual explanation of why. However, if you want to get the namespace in prototype functions, you can do this by returning the extended object from foo .

Note that I also had to convert this to Object above. This is because you cannot attach properties to primitive values, such as strings. var str = "foo"; str.bar = 1 var str = "foo"; str.bar = 1 will work, but only because JS will automatically convert "foo" to an object. However, since str refers to a primitive rather than an automatically created object, the object is immediately discarded, and we lose the bar .

+5
source

It is not possible to determine the object for which the object is a property. The same object (in your case, a function) can be attached to several objects. Here is an example:

 var myObj = {}; var objA = {prop: myObj}; var objB = {nother: myObj} 

If you provided a link to myObj, how could you know which parent you have in mind? In one case itโ€™s nothing, the other case is objA, the last case is the โ€œchildโ€ object objB. If you explain why you need this behavior, we can help you solve this problem. But the answer to this question is that you CANNOT do it.

+3
source

All Articles