js create an object


   
   
   
   
  1. <span class="string">"text/javascript"</span><span>>  </span></span></li> <li><span>    </span><span class="keyword">function</span><span> createObject(name)  </span></li> <li class="alt"><span>    {  </span></li> <li><span>        </span><span class="keyword">var</span><span> object = </span><span class="keyword">new</span><span> Object();  </span></li> <li class="alt"><span>          </span></li> <li><span>        object.name = name;  </span></li> <li class="alt"><span>          </span></li> <li><span>        object.setName = </span><span class="keyword">function</span><span>(name){  </span></li> <li class="alt"><span>            object.name = name;  </span></li> <li><span>        }  </span></li> <li class="alt"><span>          </span></li> <li><span>        </span><span class="keyword">return</span><span> object;  </span></li> <li class="alt"><span>    }  </span></li> <li><span>      </span></li> <li class="alt"><span>    </span><span class="keyword">var</span><span> obj1 = createObject(</span><span class="string">"lisi"</span><span>);  </span></li> <li><span>    alert(obj1.name);  </span></li> <li class="alt"><span>    obj1.setName(</span><span class="string">"zhaogongzuo"</span><span>);  </span></li> <li><span>    alert(obj1.name);  </span></li> <li class="alt"><span>      </span></li> <li><span> 
the last line in createObject method isthe key.That is return oject.As you see,in the createObject method,oneOOOject actuallygenenenese new function oject.This is is nowhat wewet want.It.It t thethethethethethethethethetheat t t t thethethethethethethethethethethethethethethethetheaaat t t t t t t t t t t t t t t t aatototoststststststststststststststatotototototototototototototototototototototomethod out,and link it to the createObject method.
 
   
   
   
   
  1. <span class="string">"text/javascript"</span><span>>  </span></span></li> <li><span>    </span><span class="keyword">function</span><span> createObject(name)  </span></li> <li class="alt"><span>    {  </span></li> <li><span>        </span><span class="keyword">var</span><span> object = </span><span class="keyword">new</span><span> Object();  </span></li> <li class="alt"><span>          </span></li> <li><span>        object.name = name;  </span></li> <li class="alt"><span>          </span></li> <li><span>        object.setName = setName;  </span></li> <li class="alt"><span>          </span></li> <li><span>        </span><span class="keyword">return</span><span> object;  </span></li> <li class="alt"><span>    }  </span></li> <li><span>      </span></li> <li class="alt"><span>    </span><span class="keyword">function</span><span> setName(name)  </span></li> <li><span>    {  </span></li> <li class="alt"><span>        </span><span class="keyword">this</span><span>.name = name  </span></li> <li><span>    }  </span></li> <li class="alt"><span>      </span></li> <li><span>    </span><span class="keyword">var</span><span> obj1 = createObject(</span><span class="string">"lisi"</span><span>);  </span></li> <li class="alt"><span>    alert(obj1.name);  </span></li> <li><span>    obj1.setName(</span><span class="string">"zhaogongzuo"</span><span>);  </span></li> <li class="alt"><span>    alert(obj1.name);  </span></li> <li><span>      </span></li> <li class="alt"><span> 
object.set Name=set Name.the right hand side can not add braaces、since the name itself refer to instance、not method.
 
Or use function like class in java、at least to some extent、
 
   
   
   
   
  1. function Person(username)  
  2.     {  
  3.         this.username = username  
  4.     }  
  5.       
  6.     var person1 = new Person("gongzuo");  
  7.       
  8.     alert(person1.username) 
when"new"te,the function Person generate an object and return.
Add properties through prototype.However,for frooptype,all object s share the same heap reference,so if you change one,the other change too. 
 
   
   
   
   
  1. Person.prototype.age = 27;  
  2.           
  3. alert(person1.age);