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.

 

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.

 

 

 

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();

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.

Archived

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

×
×
  • 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.