简体   繁体   中英

Assign property reference in javascript

I am working on a small javascript script to edit CSS code and I found out there are many ... particularities, if I may say so, with internet explorer in comparison to other browsers. For instance the rules object of the document.stylesheet object is called cssRule for most browsers and rule for IE.

What I would like to do here is assign the reference of a property of an object containing the size of the window (window.innerWidth & document.body.clientWidth) in order to avoid checking each time if IE object names should be used or the "normal" one.

Is it a good/bad idea?

Before posting the question I thought some more about it and came up with a solution..

function CommonObject(obj, propertyName){
    this.get = function() { return obj[propertyName]; }
}

Is there another/better way to do this?

Thanks

(Yeah I know doing this is really not necessary, especially since I'm making a small script and performance isn't really a concern but I'm mostly being curious.)

Use a mapping table:

var css = {"rules": {"ie":"rule","w3":"cssRule"} }

var browser = !!window.XPathEvaluator ? "w3" : "ie"

var foo = document.styleSheets["0"][css.rules[browser] ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM