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

  • 相关阅读:
    MySql日期与时间函数
    Redis内存分析工具redis-rdb-tools
    MYSQL5.7脚本运行时出现[Warning] Using a password on the command line interface can be insecure
    Prometheus
    Shopt命令(删除排除)
    Nginx配置跨域支持功能
    Nginx之 try_files 指令
    Grafana使用阿里云短信的报警实现
    用python发送短消息(基于阿里云平台)
    同步pod时区与node主机保持一致
  • 原文地址:https://www.cnblogs.com/mxw09/p/1710618.html
Copyright © 2011-2022 走看看