JavaScript函数、方法、对象代码
更新时间:2008年10月29日 14:31:53 作者:
函数定义可以嵌套在其他函数中,常用作子函数。但不能出现在循环或条件语句中。
函数直接量,适用于只使用一次,无需命名的函数。如下例,后者虽有fact函数名,但只用作自我调用。
var f = function(x)
{
return x*x;
}
var f = function fact(x)
{
if(x<=1) return 1;
else return x*fact(x-1);
};
函数的参数数组:Arguments对象。常用arguments[i]引用,arguments.length等。
对象:
对象定义(函数)中的方法,其实也是个函数,与嵌套函数不同点在于:通过关键字this引用对象实体。
function Rectangle(w, h)
{
this.width = w;
this.height = h;
this.area = area;
this.enlarge = Rectangle_enlarge;
this.setSize = setSize;
//通过构造函数定义方法
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);
}
}
原型对象和继承:
原型对象是存放方法和其他常理属性的理想场所,相当于C#中的静态字段。
复制代码 代码如下:
var f = function(x)
{
return x*x;
}
var f = function fact(x)
{
if(x<=1) return 1;
else return x*fact(x-1);
};
函数的参数数组:Arguments对象。常用arguments[i]引用,arguments.length等。
对象:
对象定义(函数)中的方法,其实也是个函数,与嵌套函数不同点在于:通过关键字this引用对象实体。
复制代码 代码如下:
function Rectangle(w, h)
{
this.width = w;
this.height = h;
this.area = area;
this.enlarge = Rectangle_enlarge;
this.setSize = setSize;
//通过构造函数定义方法
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);
}
}
原型对象和继承:
原型对象是存放方法和其他常理属性的理想场所,相当于C#中的静态字段。
相关文章
Javascript表达式中连续的 && 和 || 之赋值区别
了区分赋值表达式中出现的连续的 ‘&&’和 ‘||’的不同的赋值含义,做了一个小测试.2010-10-10Electron 打包问题:electron-builder 下载各种依赖出错(推荐)
这篇文章主要介绍了Electron 打包问题:electron-builder 下载各种依赖出错,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-07-07
最新评论