Ok, now that I think I understand how Objects work in JavaScript, I'm off to look up how to implement a Singleton pattern using JavaScript.
JavaScript Object and Prototypes
Collapse
X
-
hmmm ... a singleton :) something like this:
kind regardsCode:function MY_OBJ() { if (this.o != null) { return MY_OBJ.prototype.o; } else { MY_OBJ.prototype.o = this; } } MY_OBJ.prototype.o = null; MY_OBJ.prototype.my_func = function() {}; var o = new MY_OBJ; o.foo = 'foo'; alert(o.foo); var a = new MY_OBJ; a.foo = 'bar'; alert(a.foo); alert(o.foo);Comment
-
*lol* ... but it's quite cool ;) ... nope ... serious ... when you have a look at it it makes use of the things we talked about before ... when we first create an instance we overwrite the prototype of the class and when the second instance should be created we just return a ref to the first created object ;)
kind regardsComment
-
I was thinking the same thing.
It is pretty cool, and thanks to you it makes a lot of sense to me now.
I have a long way to go with JavaScript. As my ASP.NET applications become more complicated I'm finding that I have to use JavaScript more and more. It's a really good thing that you've helped me understand these basics now because I have a feeling everything's about to become a lot more complicated in my future.Comment
-
I started debugging my original Ajax enabled .net server control (again...becaus e it's not working in IE8... in fact it's crashing IE8 RC1). Upon researching a way around my problems I encountered numerous references to JSON with regards to these types of controls. Curious, I finally looked into JSON and now understand what you were referring to in your comment here.ok .... let's have a closer look at it :) ...
here you create an instance of an object directly and you declare the properties a and b ... where b stores a reference to the function-code that is assigned to it. note: when you have a look at this ... this is where JSON is not far away ;) just have that as a string from a response and eval it and you have a native js-object ready for use.Code:var foo = { a: 'some_value', b: function() { // whatever code } };
I find it ironic that I had to experience this problem first hand myself even though you warned me about ""this"".Comment
Comment