日期:2014-05-16  浏览次数:20424 次

求关于JS类中属性的析构函数的实现!
<script>
var   abc=new   mytable();
abc.height=100;
abc.width=400;
abc.setTablePosition();

function   mytable(){
var   self=this;
var   height;
var   width;

var   t= " <table   id= 'mytb '   border= '1 '> <tr> <td> This   is   my   table </td> </tr> </table> ";
if(document.readyState== "complete "||window.document.readyState== " "){document.body.insertAdjacentHTML( "beforeEnd ",t);}else   {document.write(t);}

this.setTablePosition=function(){
        var   f=document.all( "mytb ");
        if(f){
            f.style.height=self.height;
            f.style.width=self.width;
        };
    };
};
</script>

在以上的类中,当我修改abc.height或abc.width的值后需要执行abc.setTablePosition();后才能将这两个属性赋予给table,请问JS中能不能实现类似该属性的析构函数?也就是我只需要改变abc.height或abc.width的值,不需要再手工执行abc.setTablePosition()便能使改变直接作用于table上。

------解决方案--------------------
JS 没有初始和析构函数.
------解决方案--------------------
脚本没有析构,你可以换一种思路,不直接使用public属性赋值,而是使用两个方法:

abc.setWidth(400);
abc.setHeight(100);
------解决方案--------------------
析构肯定是没有的..

但是我以前看过篇文章...JS能模拟出构造的...好象是见过..不确定!