Using underscore like dollar sign in javascript -


i trying shorten things bit, making function implements this

element.prototype.methodname =  function(selector) {    return this.queryselector(selector); } 

it's okay when tried use $ or other letters or words, seems underscore not working it, knowing underscores _ valid variable names.

element.prototype._ =  function(selector) {    return this.queryselector(selector); } 

called way : _('body')

error message : referenceerror: _ not defined

assuming have following html:

<div id="foo"></div> 

and following javascript code:

var foo = document.getelementbyid('foo');  element.prototype._ = function() {     this.innerhtml = "bar"; }  foo._(); 

everthing works fine expected, @ least in google chrome. did not test other browser. way can extend elements desired function. of course need fetch element first, call _(); still gives reference error. if you want function named _ write:

function _() {     // } 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -