<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践理解]js call  方法使用及摘录]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Wed, 26 Oct 2011 03:10:18 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	缘起：<br/>&nbsp;&nbsp; 发现一兄弟写的代码如下：我一查Jquery没有查到，Js没学好，哈，原来是Js的函数：<br/><br/><textarea name="code" class="html" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;$(&quot;#maindiv .busidiv:last&quot;).find(&quot;#chooseBusiInfo&quot;).change(function()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(this.value)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getBasicInfo.call(this);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
</textarea><br/><br/>于是，探讨后他写出如下，我自己改造了下，这个call，<br/><textarea name="code" class="html" rows="15" cols="100">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;script langrage=&quot;javascript&quot;&gt;
window.name=&quot;studyandask&quot;;//下面的helo默认是window对象
var obj=&#123;&quot;name&quot;:&quot;dony&quot;&#125;;
var obj2=&#123;&quot;name&quot;:&quot;jack&quot;&#125;;
function helo()
&#123;
&nbsp;&nbsp;alert(this.name);
&#125;
helo();//弹出：studyandask
helo.call(obj);//弹出：dony
helo.call(obj2);//弹出：jack
//加1个参数，这个有点相当于那个C语言的argv

function helo2(par)
&#123;
&nbsp;&nbsp;alert(this.name);
&nbsp;&nbsp;alert(typeof(par));//如果不用call，这个返回是Obj的，特别注意。
&nbsp;&nbsp;alert(par);
&#125;
var par = &#039;test&#039;;
alert(&quot;多参数传递：&quot;);
helo2.call(obj2,par); //一定不要忘记了有个call， 我老忘记哇。
&lt;/script&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Js的call 学习&lt;/title&gt;
&lt;/head&gt;
编程也不光是一种体力活，最重要是知道怎么实现某功能集合，但这并不是等于就不了解其具体实现及用法，而是得深入了解其用法乃至于本质和本身适用范围，实质是体力脑力接合。--By:jack
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

</textarea><br/><br/>网上有博客说这个call，补充如下，原文URL丢失了，抱歉，粘贴如下：<br/><br/><br/>js call&nbsp;&nbsp;方法<br/>请参阅<br/>应用于：Function 对象<br/>要求<br/>版本 5.5<br/>调用一个对象的一个方法，以另一个对象替换当前对象。<br/><br/>call([thisObj[,arg1[, arg2[, [,.argN]]]]])<br/>参数<br/>thisObj<br/>可选项。将被用作当前对象的对象。<br/>arg1, arg2, , argN<br/>可选项。将被传递方法参数序列。<br/>说明<br/>call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。<br/><br/>如果没有提供 thisObj 参数，那么 Global 对象被用作 thisObj。<br/><br/>-------------------------------------------------------------------------------------------<br/>乍一看，很容易把人看迷胡，先做一些简单的说明<br/>obj1.method1.call(obj2,argument1,argument2)<br/>如上，call的作用就是把obj1的方法放到obj2上使用，后面的argument1..这些做为参数传入．<br/><br/>举一个具体的例子<br/><textarea name="code" class="html" rows="15" cols="100">function add(a,b)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;alert(a+b);
&#125;
function sub(a,b)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;alert(a-b);
&#125;

add.call(sub,3,1);</textarea><br/><br/>这个例子中的意思就是用 add 来替换 sub，add.call(sub,3,1) == add(3,1) ，所以运行结果为：alert(4); // 注意：js 中的函数其实是对象，函数名是对 Function 对象的引用。<br/><br/><br/>看一个稍微复杂一点的例子<br/><textarea name="code" class="html" rows="15" cols="100">function Class1()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;this.name = &quot;class1&quot;;

&nbsp;&nbsp;&nbsp;&nbsp;this.showNam = function()
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(this.name);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;

function Class2()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;this.name = &quot;class2&quot;;
&#125;

var c1 = new Class1();
var c2 = new Class2();

c1.showNam.call(c2);</textarea><br/>注意，call 的意思是把 c1 的方法放到c2上执行，原来c2是没有showNam() 方法，现在是把c1 的showNam()方法放到 c2 上来执行，所以this.name 应该是 class2，执行的结果就是 ：alert（&quot;class2&quot;）;<br/><br/>怎么样，觉得有意思了吧，可以让a对象来执行b对象的方法，这是java程序员所不敢想的。还有更有趣的，可以用 call 来实现继承<br/><br/><br/><textarea name="code" class="html" rows="15" cols="100">function Class1()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;this.showTxt = function(txt)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(txt);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;

function Class2()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;Class1.call(this);
&#125;

var c2 = new Class2();

c2.showTxt(&quot;cc&quot;);</textarea><br/>这样 Class2 就继承Class1了，Class1.call(this) 的 意思就是使用 Class1 对象代替this对象，那么 Class2 中不就有Class1 的所有属性和方法了吗，c2 对象就能够直接调用Class1 的方法以及属性了，执行结果就是：alert（“cc”）;<br/><br/>对的，就是这样，这就是 javaScript 如何来模拟面向对象中的继承的，还可以实现多重继承。<br/><br/><br/><textarea name="code" class="html" rows="15" cols="100">function Class10()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;this.showSub = function(a,b)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(a-b);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;

function Class11()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;this.showAdd = function(a,b)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(a+b);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;


function Class2()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;Class10.call(this);
&nbsp;&nbsp;&nbsp;&nbsp;Class11.call(this);
&#125;</textarea>很简单，使用两个 call 就实现多重继承了<br/>当然，js的继承还有其他方法，例如使用原型链，这个不属于本文的范畴，只是在此说明call 的用法<br/>说了call ，当然还有 apply，这两个方法基本上是一个意思<br/>区别在于 call 的第二个参数可以是任意类型，而apply的第二个参数必须是数组
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践理解]js call  方法使用及摘录]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>