JavaScript prototype object not working 3

Posted by Lloyd on May 15, 2010 at 2:46 pm  

If you happen to be working on a custom object and using the prototype property of JavaScript to add custom methods but it doesn’t seem to work, the solution is simple, you just need to instantiate your custom object by using the new keyword so all the custom methods through the prototype object are added.

// Sample custom object
var CustomObject = function() {
	this.props = {
			property: 'value'
		}
}
 
CustomObject.prototype = {
	CustomMethod: function() {
		alert(this.props.property);
	}
}
// Instantiate the custom object
var foo = new CustomObject;
// Doing that enables you to do this
foo.CustomMethod(); // alerts 'value'
 
// will not work because the object is not created as a new instance
// prototype methods not yet added to CustomObject
CustomObject.CustomMethod();

HTH :)

Comments

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Leave a response

  1. archknight79 Tue, 22 Jun 2010 16:52:34 UTC

    you might want to look for a lightwindow that works and uses prototype. http://blog.clickmike.com/?p=65 co’z the other lightbox sucks. :D

    p.s. can play almost anything :D heheh

  2. archknight79 Tue, 22 Jun 2010 16:53:36 UTC

    and by the way this post is really helpful too. Thanks Ninja Kid!

  3. Lloyd Sat, 26 Jun 2010 18:31:00 UTC

    Prototype mentioned above is not the library but the prototype property within JavaScript itself for creating custom properties to objects. http://www.w3schools.com/jsref/jsref_prototype_math.asp
    Thanks for the link, btw! :)

Comments


^ Back to Top