JavaScript Objects
JavaScript has several built-in objects, like String, Date, Array, and more.An object is just a special kind of data, with properties and methods.
Objects Properties
Properties are the values associated with an object.The syntax for accessing the property of an object is:
objectName.propertyName
var message="Hello World!";
var x=message.length;
var x=message.length;
12
Real Life Illustration
A person is an object.The persons' properties include name, height, weight, age, skin tone, eye color, etc.
All persons have these properties, but the values of those properties differ from person to person.
Objects Have Methods
Methods are the actions that can be performed on objects.You can call a method with the following syntax:
objectName.methodName()
var message="Hello world!";
var x=message.toUpperCase();
var x=message.toUpperCase();
HELLO WORLD!
Real Life Illustration
A person is an object.The persons' methods could be eat(), sleep(), work(), play(), etc.
All persons have these methods.
Creating JavaScript Objects
With JavaScript you can define and create your own objects.There are 2 different ways to create a new object:
- 1. Define and create a direct instance of an object.
- 2. Use a function to define an object, then create new object instances.
Creating a Direct Instance
This example creates a new instance of an object, and adds four properties to it:
personObj=new Object();
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=50;
personObj.eyecolor="blue";
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=50;
personObj.eyecolor="blue";
Example
personObj={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
Using an Object Constructor
This example uses a function to construct the object:Example
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
Adding Methods to JavaScript Objects
Methods are just functions attached to objects.Defining methods to an object is done inside the constructor function:
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.changeName=changeName;
function changeName(name)
{
this.lastname=name;
}
}
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.changeName=changeName;
function changeName(name)
{
this.lastname=name;
}
}
Now You Can Try:
myMother.changeName("Doe");
Creating JavaScript Object Instances
Once you have a object constructor, you can create new instances of the object, like this:
var
myFather=new person("John","Doe",50,"blue");
var myMother=new person("Sally","Rally",48,"green");
var myMother=new person("Sally","Rally",48,"green");
Adding Properties to JavaScript Objects
You can add new properties to an existing object by simply giving it a value.Assume that the personObj already exists - you can give it new properties named firstname, lastname, age, and eyecolor as follows:
The value of x, after execution of the code above will be:
0 komentar:
Posting Komentar