Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

JavaScript - create private property and function for Object

1.Create private property

1:  function Person(first,last,age) {  
2:    this.firstname = first;  
3:    this.lastname = last;  
4:    this.age = age;  
5:    var bankBalance = 7500; // private property which could not be called outside 
6:  }  
7:  // create your Person   
8:  var john = new Person("Guangyuan","Piao",29);  
9:  // try to print his bankBalance  
10:  console.log(john.bankBalance);  // will print undefined

2.Create and call private function

1:  function Person(first,last,age) {  
2:    this.firstname = first;  
3:    this.lastname = last;  
4:    this.age = age;  
5:    var bankBalance = 7500;  
6:    var returnBalance = function() {  
7:     return bankBalance;  
8:    };  
9:    // create the new function here  
10:    this.askTeller = function () {  
11:      return returnBalance;    
12:    }  
13:  }  
14:  var john = new Person('John','Smith',30);  
15:  console.log(john.returnBalance);  
16:  var myBalanceMethod = john.askTeller();  // return function
17:  var myBalance = myBalanceMethod(); // call function
18:  console.log(myBalance);  

JavaScript - Check Object type & property exist or not

1.Check Object type

1:  var anObj = { job: "I'm an object!" };  
2:  var aNumber = 42;  
3:  var aString = "I'm a string!";  
4:  console.log(typeof anObj); // should print "object"  
5:  console.log(typeof aNumber); // should print "number"  
6:  console.log(typeof aString); // should print "string"  


2.Check property existence

1:  var myObj = {  
2:    name: "Piao Guangyuan"  
3:  };  
4:  console.log( myObj.hasOwnProperty('name') ); // should print true  
5:  console.log( myObj.hasOwnProperty('nickname') ); // should print false  

JavaScript - literal notation for adding methods in Object

1:  var james = {  
2:    job: "programmer",  
3:    married: false,  
4:    speak: function( msg ) {  
5:      console.log("Hello, I am feeling "+msg);  
6:    }  
7:  };  
8:  james.speak("great");  
9:  james.speak("just okay");  

JavaScript - Object Array

 // Our Person constructor  
 function Person(name,age){  
   this.name = name;  
   this.age = age;  
 }  
 // Now we can make an array of people  
 var family = [];  
 family[0] = new Person("alice",40);  
 family[1] = new Person("bob",42);  
 family[2] = new Person("michelle",8);  
 family[3] = new Person("timmy", 6);  
 // loop through our new array  
 for(var i=0;i<family.length;i++){  
   console.log(family[i].name);    
 }  

JavaScript - Object Constructor with properties and functions

 function Rectangle(height, width) {  
  this.height = height;  
  this.width = width;  
  this.calcArea = function() {  
    return this.height * this.width;  
  };   
  this.calcPerimeter = function() {  
   return 2*(this.height+this.width);    
  }  
 }  
 var rex = new Rectangle(7,3);  
 var area = rex.calcArea();  
 var perimeter = rex.calcPerimeter();  

JavaScript - Object property for creating contact list

1:  var friends = new Object();  
2:  friends.steve = {  
3:    firstName : 'Steve',  
4:    lastName : 'Shock',  
5:    number : 13533334444,  
6:    address : ['20','Rahoon']  
7:  };  
8:  friends.bill = {  
9:    firstName : 'Bill',  
10:    lastName : 'Guangyuan',  
11:    number : 13514437556,  
12:    address : ['26','Angues House']  
13:  };  
14:  function list(friends){  
15:    for (var p in friends) {  
16:      console.log(p);    
17:    }  
18:  };  
19:  var search = function (name){  
20:    for(var p in friends){  
21:      console.log(friends[p].firstName);  
22:      if(friends[p].firstName === name){  
23:        console.log(p);  
24:        return friends[p];  
25:      }  
26:    }    
27:  };  
28:  list(friends);  
29:  search('Steve');