Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend Javascript Form object with prototype

Is there any chance to use prototype on a Form object, this is not working:

Form.prototype.myFunc=function()
{
  alert('OK!');
}

On the other hand, String objects are extendable, for example:

String.prototype.trim = function() {
  return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
like image 735
user1517081 Avatar asked Jul 11 '12 08:07

user1517081


People also ask

How do you extend an object in JavaScript?

The extends keyword can be used to extend the objects as well as classes in JavaScript. It is usually used to create a class which is child of another class. Syntax: class childclass extends parentclass {...}

What is the difference between __ proto __ and prototype?

All the objects have proto property. The prototype gives access to the prototype of function using function. proto gives access to the prototype of the function using the object.

How do you set the prototype of one object to another?

The Object. setPrototypeOf() method sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null. All JavaScript objects inherit properties and methods from a prototype. It is generally considered the proper way to set the prototype of an object.

Can you extend a built-in type JS?

Array, Map, and other built-in classes are extendable. The most impressive thing is that built-in methods such as map , filter, and more - return new objects of literally the inherited type NumsArray .


1 Answers

If you means HTMLFormElement, then it should be

HTMLFormElement.prototype.myFunc=function() {
  alert('OK!');
};
like image 93
xdazz Avatar answered Oct 17 '22 08:10

xdazz