I defined a prototype function ‘isElement’ of Node and I want to make it works like ‘div.isElement’ return ‘true’.
<div id="dom1">someText
<p>This is p1 of <span class="bold">dom1</span></p>
</div>
Node.prototype.isElement = (function(){
var result;
var fn = function(){
console.log(obj);
result = (this.nodeType == 1 ? true : false);
return result;
};
fn.toString = fn.valueOf = function(){
return result;
};
return fn;
})();
var dom1 = document.getElementById('dom1');
dom1.isElement(); //true
dom1.isElement; //true
If the ‘dom1’ never call the function ‘isElement()’,then ‘dom1.isElement’ return ‘undefined’. I understand why it return ‘undefined’,but I want to know how to makes it return ‘true’ or ‘false’ when ‘isElement()’ never be called.
I just want to use it like:
if(dom1.isElement){//do something}
but not like:
if(dom1.isElement()){//do something}
waiting for answers , thanks.
You can add a property that has your function as the getter: