JavaScript函數(shù)、方法、對象代碼
更新時間:2008年10月29日 14:31:53 作者:
函數(shù)定義可以嵌套在其他函數(shù)中,常用作子函數(shù)。但不能出現(xiàn)在循環(huán)或條件語句中。
函數(shù)直接量,適用于只使用一次,無需命名的函數(shù)。如下例,后者雖有fact函數(shù)名,但只用作自我調(diào)用。
var f = function(x)
{
return x*x;
}
var f = function fact(x)
{
if(x<=1) return 1;
else return x*fact(x-1);
};
函數(shù)的參數(shù)數(shù)組:Arguments對象。常用arguments[i]引用,arguments.length等。
對象:
對象定義(函數(shù))中的方法,其實也是個函數(shù),與嵌套函數(shù)不同點在于:通過關(guān)鍵字this引用對象實體。
function Rectangle(w, h)
{
this.width = w;
this.height = h;
this.area = area;
this.enlarge = Rectangle_enlarge;
this.setSize = setSize;
//通過構(gòu)造函數(shù)定義方法
function Rectangle_enlarge()
{
this.width *= 2;
this.height *= 2;
}
function setSize(width, height)
{
if(arguments.length < 2)
{
throw new Error("arguments less!");
}
else if(arguments.length >= 2)
{
this.width = width;
this.height = height;
}
}
function area()
{
return (this.width * this.height);
}
function area1()
{
alert(10);
}
}
原型對象和繼承:
原型對象是存放方法和其他常理屬性的理想場所,相當(dāng)于C#中的靜態(tài)字段。
復(fù)制代碼 代碼如下:
var f = function(x)
{
return x*x;
}
var f = function fact(x)
{
if(x<=1) return 1;
else return x*fact(x-1);
};
函數(shù)的參數(shù)數(shù)組:Arguments對象。常用arguments[i]引用,arguments.length等。
對象:
對象定義(函數(shù))中的方法,其實也是個函數(shù),與嵌套函數(shù)不同點在于:通過關(guān)鍵字this引用對象實體。
復(fù)制代碼 代碼如下:
function Rectangle(w, h)
{
this.width = w;
this.height = h;
this.area = area;
this.enlarge = Rectangle_enlarge;
this.setSize = setSize;
//通過構(gòu)造函數(shù)定義方法
function Rectangle_enlarge()
{
this.width *= 2;
this.height *= 2;
}
function setSize(width, height)
{
if(arguments.length < 2)
{
throw new Error("arguments less!");
}
else if(arguments.length >= 2)
{
this.width = width;
this.height = height;
}
}
function area()
{
return (this.width * this.height);
}
function area1()
{
alert(10);
}
}
原型對象和繼承:
原型對象是存放方法和其他常理屬性的理想場所,相當(dāng)于C#中的靜態(tài)字段。
相關(guān)文章
ionic實現(xiàn)下拉刷新載入數(shù)據(jù)功能
這篇文章主要為大家詳細介紹了ionic實現(xiàn)下拉刷新載入數(shù)據(jù)功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Javascript表達式中連續(xù)的 && 和 || 之賦值區(qū)別
了區(qū)分賦值表達式中出現(xiàn)的連續(xù)的 ‘&&’和 ‘||’的不同的賦值含義,做了一個小測試.2010-10-10Electron 打包問題:electron-builder 下載各種依賴出錯(推薦)
這篇文章主要介紹了Electron 打包問題:electron-builder 下載各種依賴出錯,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07