js create an object
7389 ワード
<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.
<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、
- function Person(username)
- {
- this.username = username
- }
-
- var person1 = new Person("gongzuo");
-
- 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.
- Person.prototype.age = 27;
-
- alert(person1.age);