Jump to content

what is the prototype?


runeveryday

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.