miércoles, 27 de marzo de 2013

Creating Namespace in javascript

The Simple HTML


Prueba Namespace
 




The javascript code

var Entities = {};  //<-- the NameSpace

Entities.Person = (function(){ //<-- create a class into the NameSpace
 var Person = function() {
  name = 'Pepe';
  age = 0;
  setAge  = function(age){
   this.age = age;
  };
  setName = function(name){
   this.name = name;
  };
  getName = function(){
   return this.name;
  };
  getAge = function(){
   return this.age;
  };
  
  return {   //<-- set the public & private methods
   setName : setName,
   getName : getName,
   setAge : setAge,
   getAge : getAge
  }
 }
 return Person; //<-- it´s very import for instancing the objects into the NameSpace
})( );

Entities.Client = (function(){ //<-- the same name atribute
 var Client = function(){  //<-- the same name object
  var name = '';
  var direction = '';
  var money = 103;
  return {

  }
 }
 return Client; //<-- the same name return type
})();

var student = new Entities.Person();  //<-- create an object into the NameSpace Entities

 student.setAge(19);
 student.setName('Juan');

 console.log('name: ' + student.name + ' get Name: ' + student.getName());
 console.log('age: ' + student.age + ' get Age: ' + student.getAge());

var foo = new Entities.Person();
foo.setName('Pepe');
foo.setAge(30);

console.log('age:' + foo.getAge() + ' name: ' + foo.getName());



var clientOne = new Entities.Client();
clientOne.name = 'ClientOne';
clientOne.money = 234;
console.log('name Client : ' + clientOne.name);
console.log('money client : ' + clientOne.money);

No hay comentarios:

Publicar un comentario