Define and use object : Objects Object Oriented « Language Basics « JavaScript DHTML






Define and use object

 

<!-- 
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
-->


function coworker(name, age) {
    this.name = name;
    this.age = age;
}

var emp1 = new coworker("Alice", 23);
var emp2 = new coworker("Fred", 32);

----------

var emp1 = {name:"Alice", age:23};
var emp2 = {name:"Fred", age:32};

----------

function showAll() {
    alert("Employee " + this.name + " is " + this.age + " years old.");    
}

function coworker(name, age) {
    this.name = name;
    this.age = age;
    this.show = showAll;
}

var emp1 = {name:"Alice", age:23, show:showAll};
var emp2 = {name:"Fred", age:32, show:showAll};

emp1.show();

----------

function coworker(name, age) {
    this.name = name;
    this.age = age || 0;
    this.show = showAll;
}

----------

function verify(obj) {
    alert("Just added " + obj.name + ".");
}
function coworker(name, age) {
    this.name = name;
    this.age = age;
    this.show = showAll;
    verify(this);
}

----------

var employeeDB = new Array();
employeeDB[employeeDB.length] = new coworker("Alice", 23);
employeeDB[employeeDB.length] = new coworker("Fred", 32);
employeeDB[employeeDB.length] = new coworker("Jean", 28);
employeeDB[employeeDB.length] = new coworker("Steve", 24);

----------

var employeeDB = new Array();
employeeDB[employeeDB.length] = {name:"Alice", age:23, show:showAll};
employeeDB[employeeDB.length] = {name:"Fred", age:32, show:showAll};
employeeDB[employeeDB.length] = {name:"Jean", age:28, show:showAll};
employeeDB[employeeDB.length] = {name:"Steve", age:24, show:showAll};

----------

var employeeDB = [{name:"Alice", age:23, show:showAll},
                  {name:"Fred", age:32, show:showAll},
                  {name:"Jean", age:28, show:showAll},
                  {name:"Steve", age:24, show:showAll}];

----------

function findInAgeGroup(low, high) {
    var result = new Array();
    for (var i = 0; i < employeeDB.length; i++) {
        if (employeeDB[i].age >= low && employeeDB[i].age <= high) {
            result = result.concat(employeeDB[i].name);
        }
    }
    return result;
}


           
         
  








Related examples in the same category

1.Object utility: create, parse and profile
2.Define Object, use its instance
3. Creating an Object and Using Object Instance Properties and Methods
4. Object-Oriented Planetary Data Presentation
5.The Book Object Definition
6.Complete Example of Using the employee, client, and project Objects
7.Source Code for the showBook() Example
8.Using the Book Object Constructor
9.Creating Objects Dynamically
10.Object to array
11.Utility class for JavaScript class definition
12.Create an object and add attributes
13.Use for in loop to display all attributes from an object
14.Display the properties from an object one by one
15.An object and its constructor
16.Use function as the object constructor