spacer
Yehuda Shiran February 15, 2000
Creating Objects
Tips: February 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

One of the first topics you need to learn when learning a new language is how to allocate memory for your data structures. In JavaScript, the operator new is the memory allocator which you would use in conjunction with creating new objects. All intrinsic JavaScript objects begin with a capital letter. For example, in order to create a new array of length 9, you would write the following:

arr = new Array(9);

Here's an example of how to create a constructor function:

anim1 = new animation("ball1");

where animation is a user-defined function. Here is an example:

function animation(id) {
  this.element = (NS4) ? document[id] : document.all[id].style;
  this.active = 0;
  this.timer = null;
  this.path = null;
  this.num = null;

  this.name = id + "Var";
  eval(this.name + " = this");

  this.animate = animate;
  this.step = step;
  this.show = show;
  this.hide = hide;
  this.left = left;
  this.top = top;
  this.moveTo = moveTo;
  this.slideBy = slideBy;
  this.slideTo = slideTo;
  this.circle = circle;
}

Notice the usage of the this prefix which gives you a reference to the object's father.

Learn more about memory allocation in Column 18, JavaScript Animation, Part I.


People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger