www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons JavaScript Objects and Classes 2 Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Lesson 45
Lesson 46
Lesson 47
Lesson 48
Lesson 49

In JavaScript, besides working with the built-in objects, we can also create our own objects.

In JavaScript there are 2 different ways to create new objects. We can either:

1. Define and create a direct instance of an object or
2. Use a function to define an object, then create new object instances.

1. Defining and creating a direct instance of an object
The following example defines and creates a direct instance of an object:

student = new Object();
student.firstname = "Danial";
student.age = 15;
student.course = "Web Designing ";

The above example creates an object named student and then adds three properties to it. Another way to write the above code is as follows:

student = {firstname:"Danial", age:15, course:"Web Designing "};

2. Using a function to define an object and then creating object instances

The following example uses a function, sometimes also referred to as a constructor, to construct the object:

function student(firstname, age, course)
{
this.firstname = firstname;
this.age = age;
this.course = course;
}

'This' in the above example is a keyword that refers to the current instance, the instance that is being created.

After defining a constructor function, we can create objects as under:

var student1 = new student("Danial", 15, "Web Designing");

Next >>> Lesson No. 20: JavaScript Objects and Classes 3





Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.