简体   繁体   中英

How to add a new method to Dojo?

Could you please tell how to add a new method to Dojo?

It works for me, but only for NodeList :

dojo.extend(dojo.NodeList, {
    foo: function() {
        alert(1)
    }
});

dojo.byId("foo").foo();

But, i need for Element :

dojo.byId("id").myMethod();

The dojo.byId function is just an alias for document.getElementById . Therefore it returns a vanilla domNode, and what you suggest would be adding a new method to Element, not Dojo.

Dojo intentionally doesn't change Element, because it's considered bad practice to do so by some (it may collide with other frameworks, for example).

If you want to do it, you can add functions to Element's prototype:

Element.prototype.myMethod = function() { 
    alert("My content is: " + this.innerHTML);
};

Then you can do:

dojo.byId("id").myMethod();

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