/    Sign up×
Community /Pin to ProfileBookmark

What really happens at function declaration and instantiation

I have written a short document describing what happens at function declaration and instantiation in Javascript:

[url]https://docs.google.com/document/d/1VvqqO2LMmilLbIvcU2JHI9ifaoMs-DB-farEzwuVuwM/edit?hl=da[/url]

comments and corrections are more than welcome – the document is open for editing.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@twseitexAug 12.2011 — Hi,

standard javascript in oldest version

var ptFunction = new Function(...)



-------------------------------------------------

Microsoft JScript (not JavaScript) in old version



Advanced Object Creation

A constructor is a function you call to instantiate and initialize a particular type of object. You invoke a constructor with the new keyword. Here are a few examples of using constructors.

var myObject = new Object(); // Creates a generic object with no properties.

var myBirthday = new Date(1961, 5, 10); // Creates a Date object.

var myCar = new Car(); // Creates a user defined object, and initializes its properties.

The constructor is passed a reference to a newly created empty object as the value of the special this keyword. It is then responsible for performing appropriate initialization for the new object (creating properties and giving them initial values). When completed, the constructor returns a reference to the object it constructed.

Writing Constructors

You can create objects and initialize them using the new operator in conjunction with predefined constructor functions such as Object(), Date(), and Function(). A powerful feature of object-oriented programming is the ability to define custom constructor functions to create custom objects for use in your scripts. You create custom constructors so you can create objects with properties already defined. Here is an example of a custom constructor (note the use of the this keyword).

function Circle (xPoint, yPoint, radius) {

this.x = xPoint; // The x component of the center of the circle.

this.y = yPoint; // The y component of the center of the circle.

this.r = radius; // The radius of the circle.

}

When you invoke the Circle constructor, you supply values for the circle's center point and the radius (these elements are all that is needed to completely define a unique circle object). You end up with a Circle object that contains three properties. Here is how you would instantiate a Circle object.

var aCircle = new Circle(5, 11, 99);

Using Prototypes to Create Objects

When you write a constructor, you can use properties of the prototype object (which is itself a property of every constructor) to create inherited properties, and shared methods. Prototype properties and methods are copied by reference into each object of a class, so they all have the same values. You can change the value of a prototype property in one object, and the new value overrides the default, but only in that one instance. Other objects that are members of the class are not affected by the change. Here is an example that makes use of the custom constructor, Circle (note the use of the this keyword).

Circle.prototype.pi = Math.PI;

function ACirclesArea () {

return this.pi * this.r * this.r; // The formula for the area of a circle is Ïr2.

}

Circle.prototype.area = ACirclesArea; // The function that calculates the area of a circle is now a method of the Circle Prototype object.

var a = ACircle.area(); // This is how you would invoke the area function on a Circle object.

Using this principle, you can define additional properties for predefined constructor functions (which all have prototype objects). For example, if you want to be able to remove leading and trailing spaces from strings (similar to VBScript's Trim function), you can create your own method on the String prototype object, and all strings in your script will automatically inherit the method.

// Add a function called trim as a method of the prototype

// object of the String constructor.

String.prototype.trim = function()

{

// Use a regular expression to replace leading and trailing

// spaces with the empty string

return this.replace(/(^s*)|(s*$)/g, "");

}

// A string with spaces in it

var s = " leading and trailing spaces ";

// Displays " leading and trailing spaces (35)"

window.alert(s + " (" + s.length + ")");

// Remove the leading and trailing spaces

s = s.trim();

// Displays "leading and trailing spaces (27)"

window.alert(s + " (" + s.length + ")");
×

Success!

Help @loldrup spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...