Extend Javascript Form object with prototype - Stack Overflow

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

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*$/, '');
}

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*$/, '');
}
Share Improve this question asked Jul 11, 2012 at 8:15 user1517081user1517081 9652 gold badges11 silver badges30 bronze badges 1
  • since there is no Form object - no, it doesn't work. – Otto Allmendinger Commented Jul 11, 2012 at 8:19
Add a ment  | 

2 Answers 2

Reset to default 9

If you means HTMLFormElement, then it should be

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

There is no specification that requires DOM objects to implement any kind of inheritance, much less prototype inheritance. Having said that, many browsers do but it is not standardised or universally implemented.

You might like to read What’s wrong with extending the DOM.

In browsers that do implement a prototype inheritance scheme for DOM elements, you can try extending HTMLFormElement.prototype using something like:

if (typeof HTMLFormElement == 'object' && 
    typeof HTMLFormElement.prototype == 'object') {

    // extend HTMLFormElement.prototype

}

Note however that the behaviour of host objects is entirely implementation dependant. The above may do anything, including throw errors.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742294160a4416776.html

相关推荐

  • Extend Javascript Form object with prototype - Stack Overflow

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

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信