background preloader

JavaScript

Facebook Twitter

A JavaScript Module Pattern. Eric Miraglia (@miraglia) is an engineering manager for the YUI project at Yahoo.

A JavaScript Module Pattern

Eric has been at Yahoo since 2003, working on projects ranging from Yahoo Sports to YUI. For the past several years, Eric and his colleagues on the YUI team have worked to establish YUI as the foundation for Yahoo's frontend engineering work while open-sourcing the project and sharing it with the world under a liberal BSD license. Eric is an editor and frequent contributor to YUIBlog; his personal blog is at ericmiraglia.com.

Creating objects. Netscape 4, IE 4 on Windows and Mac, and IE 5 on Mac do not support the operator.

Creating objects

Any function in JavaScript can be used to create custom object classes, simply by calling it using the keyword new . When called in this way, the special variable inside the function references the new object that is being constructed (it normally refers to the 'current' object, which is usually , except inside methods). The function should not return a value. The following function can be used as demonstrated to create an object of class : function myobject() { this.containedValue = 0; this.othercontainedValue = 0; this.anothercontainedValue = 0; } var mything = new myobject();