<html>
<body>
<script>
function Box()
{
this.Width = 100;
this.Height = 80;
this.Multiply = function()
{
alert(this.Width * this.Height);
}
this.Display = function ()
{
//document.write("<input type='button' onclick='this.Multiply()' value='Click'>");
var thisRef = this;
var btn = document.createElement("INPUT");
btn.type="button";
btn.value="Click";
btn.attachEvent("onclick",function(){thisRef.Multiply();});
document.body.appendChild(btn);
}
}
/* ****** */
var obj = new Box();
obj.Display();</script></body >
</html>

解决方案 »

  1.   

    <html>
    <body>
    <script>
    function Box(instance)
    {
    this.Width = 100;
    this.Height = 80;
    this.instance = new String(instance);
    this.Multiply = function()
    {
    alert(this.Width * this.Height);
    }
    this.Display = function ()
    {
    if(window[instance] != this){
    throw new Error(-1,"对不起,创建对象时请把对象名称当字符串传进来.");
    }
    document.write("<input type='button' onclick='" + this.instance + ".Multiply()' value='Click'>");
    }
    }/* ****** */
    var obj = new Box("obj");
    obj.Display();
    </script></body >
    </html>
      

  2.   

    document.write("<input type='button' onclick='new Multiply()' value='Click'>");