最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

javascript面向对象特性代码实例_javascript技巧

来源:动视网 责编:小采 时间:2020-11-27 21:24:22
文档

javascript面向对象特性代码实例_javascript技巧

javascript面向对象特性代码实例_javascript技巧:一、基本的类的使用方法一: 代码如下:function sth(a) // 构造函数 { this.a = a; this.fun = output; // 成员函数 } function output(a, b, c) { document.write(this.a); } //调用var s = new sth(250)
推荐度:
导读javascript面向对象特性代码实例_javascript技巧:一、基本的类的使用方法一: 代码如下:function sth(a) // 构造函数 { this.a = a; this.fun = output; // 成员函数 } function output(a, b, c) { document.write(this.a); } //调用var s = new sth(250)


一、基本的类的使用
方法一:
代码如下:
function sth(a) // 构造函数
{
this.a = a;
this.fun = output; // 成员函数
}

function output(a, b, c)
{
document.write(this.a);
}

//调用
var s = new sth(250);
s.fun(1, 2, 3);
ouput(1, 2, 3); //如果output在sth之前就是错的

方法二:
代码如下:function sth(a)
{
this.a = a;
this.output = function()
{
document.write(this.a);
}
}

var s = new sth(2);
s.output(); // 输出2
二、继承
方法一:
代码如下:function A(x)
{
this.x = x;
}

function B(x, y)
{
// 方法1
/*
this.construct = A;
this.construct(x);
delete this.construct;
*/

// 方法2
//A.call(this, x);

// 方法3
A.apply(this, new Array(x)); // 亦可A.apply(this, arguments), 不过arguments参数顺序一定要对

this.y = y;
this.print = function()
{
document.write("x = ", x,
", y = ", y);
}
}

var b = new B(1, 2);
b.print();
alert(B instanceof A); // 输出false

优点:可以实现多继承(多调用call就好)

缺点:
· 必须以构造函数方式使用
· 使用instanceof运算符运算此类继承结果为false

方法二:
代码如下:function A()
{

}
A.prototype.x = 1;

function B()
{

}
B.prototype = new A(); // 不能带参数!
B.prototype.y = 2;
B.prototype.print = function()
{
document.write(this.x, ", ", this.y, "
");
}

var b = new B();
b.print();
document.write(b instanceof A); // 输出true

缺点:
· 不能实现多继承
· 构造函数不带参数

Tips

通常使用混合模式,两者一起用
代码如下:
function A(x)
{
this.x = x;
}
A.prototype.printx = function() // 写到A类里面this.printx = function....也是可以的,下同
{
document.write(this.x, "
");
}

function B(x, y)
{
A.call(this, x);
this.y = y;
}
B.prototype = new A(); // 不能带参数!
B.prototype.printxy = function()
{
document.write(this.x, ", ", this.y, "
");
}

var b = new B(1, 2);
b.printx(); // 输出1
b.printxy(); // 输出1, 2
document.write(b instanceof A); // 输出true

三、类似静态成员函数的使用
代码如下:function sth(a)
{
this.a = a;
}

sth.fun = function(s)
{
document.write(s.a);
}

var s = new sth(2);
sth.fun(s); // 输出2


四、对象的释放
代码如下:var obj = new Object; // obj是引用
obj = null; // 取消引用,会自动进行垃圾回收;如果需要根本释放此对象,要将它的所有引用都赋值为null

五、函数对象
代码如下:var v = new Function("arg1", "arg2", "document.write(arg1 + arg2);"); // 定义一个函数对象,参数分别是arg1,arg2
v(1, 2); // 将会输出3

六、回调函数
代码如下:function callback(func, arg)
{
func(arg);
}

function fun(arg)
{
document.write(arg);
}

//callback(func, "sb"); // 这种做法不行

var func = new Function("arg", "fun(arg);");
// 当然也可以把func(arg)换成具体的执行代码,
// 但是函数代码庞大了就最好这样做了
callback(func, "sb");

七、函数的重载
代码如下:function fun()
{
switch (arguments.length)
{
case 1:
document.write(arguments[0]);
break;
case 2:
document.write(arguments[0] + arguments[1]);
break;
default:
document.write("ERROR!");
break;
}
}

fun(1);
fun(1, 2);

八、利用函数闭包实现有“静态变量”的函数
代码如下:function fun()
{
var v = 1;
function fun2()
{
++v;
document.write(v);
document.write("
");
return v;
}

return fun2;
}

var func = fun();
func(); // 输出2
func(); // 输出3
func(); // 输出4

文档

javascript面向对象特性代码实例_javascript技巧

javascript面向对象特性代码实例_javascript技巧:一、基本的类的使用方法一: 代码如下:function sth(a) // 构造函数 { this.a = a; this.fun = output; // 成员函数 } function output(a, b, c) { document.write(this.a); } //调用var s = new sth(250)
推荐度:
标签: 特征 js 代码
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top