第一種方式:使用apply()方法: function sum(x, y) { alert(this); return x + y;//這里的this指的是callS對象方法 } function callS() { callS.callSum1(1, 2); } callS.callSum1 = function (x, y) { alert(this);//這里的this是callS方法 var s = sum.apply(this, arguments); return s; } callS(); 第二種放方法:使用call()方法: 有關call方法: window.color="red"; var o ={color:"blue"}; function sayColor(a,b){alert(this.color);} sayColor();//red sayColor.call(this,1,2);//red sayColor.call(window,1,2);//red sayColor.call(o,1,2);//blue l在使用call()方法時,必須明確地傳入每一個參數。結果和apply一樣。 其實apply和call真正的強大用途在於,能夠擴充函數賴以運作的作用域: 這樣擴充的最大好處,就是對象不需要與方法有任何耦合關系。 第三種方法:使用new關鍵字: function Person(name,age){ this.name=name; this.age=age; this.sayName=function(){alert(this.name);}; } var p1 = new Person("james",27); var p2 = new Person("coook",24); p1.sayName(); p2.sayName(); lnew關鍵字: 0開辟堆空間 1創建對象;2將構造函數作用域賦值給新對象(this就指向了該新對象);3執行構造函數中的代碼(為新對象添加屬性);4返回新對象