zoukankan      html  css  js  c++  java
  • Firefox下使用getter和setter

    在火狐下运行:
    o = {  
         value:9
         
     }  
     Object.prototype.__defineGetter__("mm",function(){return "属性测试"})
     alert(o.mm)


    在火狐下运行,可以扩展innerText
    第一种:
    if(typeof(HTMLElement)!="undefined" && !window.opera)   
     {   
       
      HTMLElement.prototype.__defineGetter__("innerText",function () { 
        return this.textContent;
       }
      ); 
      
      HTMLElement.prototype.__defineSetter__("innerText",function (setValue) { 
        this.textContent = setValue;    
      });
     }
    第二种:
    if(typeof HTMLElement!="undefined"){
            HTMLElement.prototype.innerText
            getter = function(){
                    var tmp = this.innerHTML.replace(/<br>/gi,"\n");
                    return tmp.replace(/<[^>]+>/g,"");
            }

            HTMLElement.prototype.innerText
            setter = function(txtStr){
                    var parsedText = document.createTextNode(txtStr);
                    this.innerHTML = "";
                    this.appendChild( parsedText );
            }
    }


    使用getter和setter(尽量不要这样使用,因为 在JavaScript 1.5的开发过程中,有一种简短的使用类似getter = 或者 setter = 定义对象的方法。这样的语法在JavaScript 1.5版本中会获

    得一个警告,以后的版本也不会支持,所以我们要避免这样的语法出现。):
    var o = {
       a:7,
       //下面两句是其实是实现对象的num属性
       get num() { return this.a+1; },
       set num(x) { this.a = x/2; }
     };
     o.num=100;
     alert(o.num)//在火狐中弹出51

  • 相关阅读:
    网卡驱动-BD详解(缓存描述符 Buffer Description)
    break&&continue
    C++解析六-继承
    C++解析五-this 指针,指向类的指针
    1-find
    C++解析四-友员函数、内联函数、静态成员
    树形结构节点求和,以及set排序
    java.util.ConcurrentModificationException 解决和for循环时对集合remove操作
    eclipse保存卡死和内存溢出
    关于Eclipse安装了反编译插件,无法查看源码问题
  • 原文地址:https://www.cnblogs.com/mxw09/p/1710618.html
Copyright © 2011-2022 走看看