js技巧--创建class时调用prototype原型方法

本文介绍了一种在JavaScript中调用实例方法的三种方式:使用eval方法、通过变量引用this、直接在HTML中使用实例名字符串。并提供了一个具体示例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一般情况下有三种方法:
1、用eval方法。
2、先把this的引用传递给一个变量。
3、在创建HTML里可以直接用instance的字符串。
更详细的请看示例。

<SCRIPT LANGUAGE="JavaScript">
<!--
function myClass(instanceName)
{
 this.instanceName = instanceName;
 this.instance = this;
 return this;
};
myClass.prototype.toAlert=function()
{
 eval(this.instanceName).callback(); // the first method to call prototype function.
 this.instance.callback(); // the second method to call prototype function.

 // the third method to call prototype function.
 document.write("<a href='javascript:void(0);' onclick='" + this.instanceName + ".callback();'>instance call prototype function.</a>")
};
myClass.prototype.callback=function()
{
 alert("blueDestiny, never-online");
};
var a = new myClass("a");
a.toAlert();
//-->
</SCRIPT>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值