Java Script Objects & Cookies

Topics: Object-oriented programming, Perl, Object Pages: 5 (732 words) Published: August 29, 2013
JavaScript Objects

Everything is an Object

In JavaScript almost everything is an object. Even primitype datatypes (exept null and undefined) can be treated as objects. •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

JavaScript 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!";
var x=message.length;

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!";
var x=message.toUpperCase();

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

person=new Object();

Alternative syntax (using object literals):

Using an Object Constructor

function person(firstname,lastname,age,eyecolor)

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");

JavaScript Classes

JavaScript is an object oriented language, but JavaScript does not use classes.

In JavaScript you don’t define classes and create objects from these classes (as in most other object oriented languages).

JavaScript is prototype based, not class based. Loop

The JavaScript statement loops through the properties of an object. Syntax
for (variable in object)
  code to be executed

var person={fname:"John",lname:"Doe",age:25};

for (x in person)
  txt=txt + person[x];

JavaScript Cookies

A cookie is a variable that is stored on the visitor's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With JavaScript, you can both create and retrieve cookie values.

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

function setCookie(c_name,value,exdays)
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...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay on Java Script
  • cookies Essay
  • Java Script Essay
  • Java Essay
  • Cookies Essay
  • Object-oriented Programming and Java Statement Essay
  • Java Script: A Programming Language Essay
  • Java Essay

Become a StudyMode Member

Sign Up - It's Free