runeveryday Posted July 1, 2010 Share Posted July 1, 2010 i want to learn javascript recently,now,there is one concepts i can't understand. which is the prototype, in javascript, there are two things about this. one is a framework. the other which is confused me a lot. Quote Link to comment Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 I could, in one script create an object and give it some properties. var foo = { a: 'blah' } I could then later add a method to the object... foo.prototype.bar = function() { alert(this.a); } Its a method of inheritance. Quote Link to comment Share on other sites More sharing options...
runeveryday Posted July 1, 2010 Author Share Posted July 1, 2010 I could, in one script create an object and give it some properties. var foo = { a: 'blah' } I could then later add a method to the object... foo.prototype.bar = function() { alert(this.a); } Its a method of inheritance. want to put your code in firebug,and make it can work, have some tries,but failed. i don't know how to Instantiate the anonymous function. Quote Link to comment Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 Actually, I'm thinking now that the 'prototype' property is only available on native objects. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted July 1, 2010 Share Posted July 1, 2010 Try this: // create foo constructor var foo = function(a) { // set property this.a = a ; } // add bar function to foo foo.prototype.bar = function() { alert(this.a); } // instantiate foo object var instance = new foo('hello'); instance.bar(); Quote Link to comment Share on other sites More sharing options...
runeveryday Posted July 1, 2010 Author Share Posted July 1, 2010 Try this: // create foo constructor var foo = function(a) { // set property this.a = a ; } // add bar function to foo foo.prototype.bar = function() { alert(this.a); } // instantiate foo object var instance = new foo('hello'); instance.bar(); i am sorry,it doesn't show any result. Quote Link to comment Share on other sites More sharing options...
runeveryday Posted July 1, 2010 Author Share Posted July 1, 2010 instance.bar(); what about this line means? Quote Link to comment Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 Indeed it does. You should get a pop-ups saying 'hello'. I forgot it only works with objects literals. That's those instantiated using the 'new' keyword. Quote Link to comment Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 instance.bar(); what about this line means? This line.... var instance = new foo('hello'); Creates an instance of the 'foo' object called 'instance'. this line.... instance.bar(); then calls the bar() method of that object. Quote Link to comment Share on other sites More sharing options...
runeveryday Posted July 1, 2010 Author Share Posted July 1, 2010 i am very glad about this. it's OK. many many thanks for your explaination. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.