Document.prototype.ad=function(){ //code }

document.prototype.ad=function() {
// code 
}

How can I write a code and call function?

replace // code with the code for the function

call it using document.prototype.ad()

If I write

HTMLDocument.prototype.ad or
document.constructor.prototype.ad or
document.ad

and call
document.ad()
it is working.

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript </h1>


<script>

document.constructor.prototype.ad2=function() {
alert(this); 
}

document.ad2(); // object HTMLDocument

HTMLDocument.prototype.ad3=function() {
alert(this + "   mesaj "); 
}

document.ad3(); // object HTMLDocument    mesaj


document.ad4=function() {
alert(this + "    4"); 
}

document.ad4(); // object HTMLDocument     4

/*

document.prototype.ad=function() {
alert(this); 
}

// document.prototype.ad(); // nothing happens
*/
</script>

</body>
</html>

I want to write “metin” instead of “innerHTML” . Message box displays “Burada paragraf var.”
I try a code but nothing happens.

<!DOCTYPE html>
<html>
<body>

<p id="demo">Burada paragraf var. </p>

<script>

document.ad=function(isim) {

return this.getElementById(isim);

}
alert(document.ad('demo'));  // object HTMLParagraphElement
alert(document.ad('demo').innerHTML); // Burada paragraf var.

// I want I write "metin" instead of "innerHTML" . Message box displays "Burada paragraf var." 
// I try this
document.ad.prototype = { metin: this(isim).innerHTML };

alert(document.ad('demo').metin);  // nothing happens

</script>

</body>
</html>

How can I do this?

You can’t invent your own language like that.

You could extend the element prototype with a function to return the innerHTML property like this:

Element.prototype.metin = function() {
    return this.innerHTML;
};

alert(document.getElementById('demo').metin());

use the latter. it’s better to not mess with the prototypes of built-in objects as you can’t judge the side effects this may have.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.