Everything is an Object
Booleans can be objects or primitive data treated as objects •
Numbers can be objects or primitive data treated as objects •
Strings are also objects or primitive data treated as objects •
Dates are always objects
Maths and Regular Expressions are always objects
Arrays are always objects
Even functions are always objects
An object is just a special kind of data, with properties and methods. •
Accessing Object Properties
Properties are the values associated with an object.
The syntax for accessing the property of an object is:
This example uses the length property of the String object to find the length of a string: var message="Hello World!";
Accessing Objects Methods
Methods are the actions that can be performed on objects.
You can call a method with the following syntax:
This example uses the toUpperCase() method of the String object, to convert a text to uppercase: var message="Hello world!";
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
Alternative syntax (using object literals):
Using an Object Constructor
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");
for (variable in object)
code to be executed
for (x in person)
txt=txt + person[x];
Examples of cookies:
Name cookie - The first time a visitor arrives to your web page, he or she must fill in her/his name. The name is then stored in a cookie. Next time the visitor arrives at your page, he or she could get a welcome message like "Welcome John Doe!" The name is retrieved from the stored cookie
Date cookie - The first time a visitor arrives to your web page, the current date is stored in a cookie. Next time the visitor arrives at your page, he or she could get a message like "Your last visit was on Tuesday August 11, 2005!" The date is retrieved from the stored cookie
Create and Store a Cookie
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
The parameters of the function above hold the name of the cookie, the value of the cookie, and the number of days until the cookie...
Please join StudyMode to read the full document