• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

object literal + prototyping

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the below javascript code,

String.prototype.test = {
msg : 'world',
print : function() {
alert(this + ' ' + this.msg);
}
}

var h = 'hello';
h.test.print(); // output: [object Object] world

How come 'this' doesn't equal 'hello'? How to make 'this' reference variable h i.e 'hello'?
Any expert can help?
[ October 18, 2007: Message edited by: Bear Bibeault ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The function context (this) is not an attribute of the function, but rather, of how the function is invoked. This is a very important concept in OO JavaScript. The same function can be invoked with different function contexts depending upon how it is called.

In your example, you are calling the function via its reference as print which is a property of the object literal that you assigned to test. Thus, that object literal, not the String instance is the function context.

By the way, please be sure to use UBB code tags when posting code to the forums. Please read this for more information.
[ October 18, 2007: Message edited by: Bear Bibeault ]
 
Ken Ng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So is there a way to get a reference to h? Unlike in DOM, you could use parentNode to reference the immediate ancestor.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there is no coupling. If you want the function context to be the String instance you could declare the function as a property of the String (rather than a property of a property of the String as you have) and call it via that property, or you can force it by using the Function.call() method.

To be honest, your example doesn't make a whole lot of sense to me as far as why you'd set something up in that manner, so I'm not sure what other recommendations to make.
[ October 19, 2007: Message edited by: Bear Bibeault ]
reply
    Bookmark Topic Watch Topic
  • New Topic