JavaScript函数、方法、对象
来源:互联网 发布:网站源码怎么来 编辑:程序博客网 时间:2024/04/29 21:50
函数定义可以嵌套在其他函数中,常用作子函数。但不能出现在循环或条件语句中。
函数直接量,适用于只使用一次,无需命名的函数。如下例,后者虽有fact函数名,但只用作自我调用。
var f = function(x)
{
return x*x;
}
var f = function fact(x)
{
if(x<=1) return 1;
else return x*fact(x-1);
};
{
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);
}
}
{
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函数、方法、对象
- JavaScript学习--Item8 函数,方法,构造函数调用 标签: javascript构造函数面向对象函数call
- 通过 prototype 为 JavaScript 的 String 对象添加方法(函数)
- isArray()函数(JavaScript中判断对象类型的种种方法)
- JavaScript创建对象与构造函数的实现方法
- javaScript 对象属性和函数的加入方法
- javascript函数对象的内部方法call(), apply(), bind()
- javascript 对象和函数(方法)区别和联系
- JavaScript函数对象
- javascript函数arguments对象
- javascript 函数与对象
- Javascript 函数对象
- javascript函数与对象
- javaScript 面向对象-函数
- JavaScript对象、函数、变量
- JavaScript 函数 对象 数组
- javaScript对象与函数
- JavaScript(3)函数、对象
- Java SE 6(JDK1.6)的十大新技术亮点
- access denied error
- WebSphere服务器安装配置全攻略
- const使用详解
- [转]java和C++的区别
- JavaScript函数、方法、对象
- [原创]实现与服务器时间同步的跨平台JS脚本 支持IE , firefox , Opera ,Netscape 等主流浏览器
- 关于批处理文件(.bat)
- 在Struts应用中施展AJAX魔法
- Windows下的Win32串口编程
- javascipt中如何操作xml(1) 写
- Worm.Magistr.g
- J2ME常见问题
- javascipt中如何操作xml(2) 读