国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js中function()使用方法

2019-11-20 21:23:44
字體:
來源:轉載
供稿:網友
javascript 函數不同于其他的語言,每個函數都是作為一個對象被維護和運行的。通過函數對象的性質,可以很方便的將一個函數賦值給一個變量或者將函數作為參數傳遞。在繼續講述之前,先看一下函數的使用語法:

以下是引用片段:
function func1(…){…}
var func2=function(…){…};
var func3=function func4(…){…};
var func5=new Function();
復制代碼 代碼如下:

<script type="text/javascript">

// 1, 方法調用模式
// 當一個函數被保存為對象的一個屬性時,我們稱之它為該對象的一個方法,那么this被綁定到該對象上
var myObject={
name : "myObject" ,
value : 0 ,
increment : function(num){
this.value += typeof(num) === 'number' ? num : 0;
return this;
} ,
toString : function(){
return '[Object:' + this.name + ' {value:' + this.value + '}]';
}
}
alert(myObject.increment(10).increment(20).toString()); // [Object:myObject {value:30}]


// 2, 函數調用模式
// 當一個函數并非一個對象的函數時,那么它被當作一個函數來調用,this被綁定到全局對象上。這是語言設計的一個錯誤。倘若語言設計正確,當內部函數調用時,this應該仍然綁定到外部函數的this變量上
var myObject={
name : "myObject" ,
value : 0 ,
increment : function(num){
this.value += typeof(num) === 'number' ? num : 0;
return this;
} ,
toString : function(){
return '[Object:' + this.name + ' {value:' + this.value + '}]';
},
getInfo: function(){
var self=this;
return (function(){
//return this.toString(); // 內部匿名函數中this指向了全局對象window, 輸出 [object Window]
return self.toString(); // 定義一個變量selft并給它賦值為this,那么內部函數通過該變量訪問到指向該對象的this
})();
}
}
alert(myObject.increment(10).increment(20).toString()); // [Object:myObject {value:30}]


// 3, 構造器調用模式
// JavaScript是一門基于原型繼承的語言, 這意味著對象可以直接從其他對象繼承屬性, 該語言是無類別的。
// 如果一個函數前面帶上new來調用,那么將創建一個隱藏連接到該函數的prototype成員的新對象,同時this將會被綁定到構造函數的實例上。
function MyObject(name){
this.name = name || 'MyObject';
this.value=0;
this.increment = function(num){
this.value += typeof(num) === 'number' ? num : 0;
};
this.toString = function(){
return '[Object:' + this.name + ' {value:' + this.value + '}]';
}
this.target = this;
}

MyObject.prototype.getInfo = function(){
return this.toString();
}

// 同時創建一個MyObject.prototype對象,myObject繼承了MyObject.prototype的所有的屬性, this綁定到了MyObject的實例上

var myObject = new MyObject();
myObject.increment(10);
alert(myObject.value); //10

var otherObject = new MyObject();
otherObject.increment(20);
alert(otherObject.value); //20

alert(myObject.target===myObject); // ture
alert(myObject.target.getInfo()); // [Object:MyObject {value:10}]


// 4, Apply 調用模式
// JavaScript是一門函數式的面向對象編程語言,所以函數可以擁有方法。 函數的apply方法,如同該對象擁有此方法,此時this指向該對象。
// apply接收兩個參數,第一個是要綁定的對象(this指向的對象),第二個是參數數組.
function MyObject(name){
this.name = name || 'MyObject';
this.value = 0;
this.increment = function(num){
this.value += typeof(num) === 'number' ? num : 0;
};
this.toString=function(){
return '[Object:'+this.name+' {value:'+this.value+'}]';
}
this.target=this;
}
function getInfo(){
return this.toString();
}
var myObj = new MyObject();
alert(getInfo.apply(myObj)); //[Object:MyObject {value:0}], this指向myObj
alert(getInfo.apply(window)); //[object Window], this指向window


// for and while
function func(a,b){
alert(a); // 1
alert(b); // 2

for(var i=0;i<arguments.length;i++){
alert(arguments[i]); // 1, 2, 1, 2, 3
}

var i=0;
while(i<arguments.length){
alert(arguments[i]); // 1, 2, 1, 2, 3
i=i+1;
}
}
func(1,2,3);

var i=0
for (i=0;i<=10;i++) {
document.write("The number is " + i + "<br/>")
}

</script>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚顺市| 句容市| 吴川市| 连江县| 康乐县| 汝阳县| 博兴县| 汉中市| 襄垣县| 通河县| 阜平县| 虹口区| 永兴县| 祁阳县| 肥东县| 浮梁县| 安国市| 盖州市| 临安市| 镇康县| 双牌县| 迁安市| 定结县| 宣城市| 浮梁县| 桂东县| 夏津县| 乐山市| 思茅市| 潼南县| 黄冈市| 宜良县| 和静县| 康定县| 江源县| 临夏市| 台东县| 黎川县| 汝城县| 象州县| 射洪县|