Jump to content

Accessing Grandparent elements in objects


philmasterplus

Recommended Posts

I have a number of functions that I want to attatch to all String objects.Example situation:

var m= {  subclass1: {    func1: function() {/* Whatever */},    func2: function() {/* Whatever */}  },  subclass2: {    func3: function() {/* Whatever */},    func4: function() {/* Whatever */}  },};for (var i in m) {  String.prototype[i] = m[i];}

The problem is, I want to access my functions like:

var str1="Hello!";var str1.subclass1.func1();str1.subclass2.func3();

Using the "this" keyword in functions will allow access to their parent objects. Is there a way to access GRANDPARENT objects as well?

Link to comment
Share on other sites

I have a number of functions that I want to attatch to all String objects.Example situation:
var m= {  subclass1: {    func1: function() {/* Whatever */},    func2: function() {/* Whatever */}  },  subclass2: {    func3: function() {/* Whatever */},    func4: function() {/* Whatever */}  },};for (var i in m) {  String.prototype[i] = m[i];}

The problem is, I want to access my functions like:

var str1="Hello!";var str1.subclass1.func1();str1.subclass2.func3();

Using the "this" keyword in functions will allow access to their parent objects. Is there a way to access GRANDPARENT objects as well?

"this" refers to the current context, not to a "parent". in a function bound to a prototype, "this" will refer to the object to the left of the rightmost dot.your second line is invalid, you need to use '=' with 'var', or assign a valid name.the problem with the above code it that "this" refers to an object, not to the inherant string.since it belongs to the object, it is not a string prototype anymore, and thus unreachable.you could add it as an object proto unstead of a string, and it should "trickle down".you might also try something like this:
Object.prototype.merge=function (ob) {	var o = this;	for (var z in ob) {		if (ob.hasOwnProperty(z)) {   o[z] = ob[z];  }	 }	//next return o;}   //end merge protoString.prototype.merge({	cTime: function(){ return new Date(); }		  ,	caps: function(){ return this.toUpperCase();}	,	backwards: function(){ return this.split("").reverse().join("");},	vowelCount: function(){ var t= this.match(/[aeiou]/g); return (t?t.length:0);}	})//end merge

it would be a little complicated to, as you wish, to define protos in JSON, because objects don't have scope, only functions do.That fact will prevent your using "this". It might be possible to use your syntax in firefox using depriciated function methods, but here is a solution that works cross browser:

String.prototype.class1= function(){   var that=this;   var t={	cTime: function(){ return new Date(); }		  ,	caps: function(){ return that.toUpperCase();}	,	backwards: function(){ return that.split("").reverse().join("");},	vowelCount: function(){ var t= that.match(/[aeiou]/g); if(t)return t.length;}		  }  return t;}//end class1 defvar b="rick james"alert(b.class1().caps())

note the parens. if you don't use them, the class object will compile at load time, and thus not have the correct "this".the above uses an inherited "this" from the string protoype, and makes it available to object members. this is the missing link.i think this is as close as you can get to safely do what you want.

Link to comment
Share on other sites

Thank you...you've put many things right for me.In a way, I could have used:

var str="Bye world!";str.class1_func1();

Though I think this beats the use of JSON objects as faux-namespaces...

your second line is invalid, you need to use '=' with 'var', or assign a valid name.
Everyone makes mistakes ;-)
Link to comment
Share on other sites

Thank you...you've put many things right for me.In a way, I could have used:
var str="Bye world!"; str.class1_func1();

Though I think this beats the use of JSON objects as faux-namespaces...

i wish that there was a nice hierarchical tree-like analogy for javascript, but it really defis that.you might consider the constructor to be the parent, but this does not always follow.the screwy thing is that in javascript, the situation to the left of a dot (".") affects the right side of the dot.thus, in one object "siblings" might have different prototypes and/or constructors.javascript is more like a crowded elevator than a family.it is confusing at first, i know...------ a lot of the confusion around "this" stems from the fact that the keyword "this" takes on several roles and meanings in javascript.the exact value depends upon the context where "this" is used.i can count 3 distinct contexts, there might be more:1. in general, just tossed in a script tag, "this" refers essentially to the "window" object.2. used in an object making function (ie: using the 'new' keyword), "this" refers to new object being created inside the function.3. in a prototype method (ie: String.prototype.div=function(id, style){...}), "this" refers to the object owning/calling the prototype.keep in mind that the only way to control "this" is to use it within a function.you can also use functions anywhere you would normally use a string or number in javascript - even as say, an array index. (anonymous functions)functions arrange public and private variable name control by using the var keyword:at a glance, the below functions seem identical in form and function, but theres a "little biggie": (use test() to see)
var title="welcome to my blog"//after executing this function , 'title' will be stuck on "loading..."function boot(){	 title="loading...";	alert(title)}//but after executing this function , 'title' will remain "welcome to my blog"function boot(){	var title="loading...";	alert(title);}function test(){ alert( title); }

well, i've over-posted once again.i hope this saves someone the frustration it has caused me.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...