Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何创建';点函数';?(js对象?)_Javascript_Html - Fatal编程技术网

Javascript 如何创建';点函数';?(js对象?)

Javascript 如何创建';点函数';?(js对象?),javascript,html,Javascript,Html,Document.prototype.greenify=function(){ 返回{ 样式:function(){ 返回此选项。color=“绿色”; } } }; document.getElementsByTagName(“H1”)[0].greenify() 试验 h1元素不从文档继承。原型。他们继承了: HTMLHeadingElement.prototype HTMLElement.prototype Element.prototype Node.prototype EventT

Document.prototype.greenify=function(){
返回{
样式:function(){
返回此选项。color=“绿色”;
}
}
};
document.getElementsByTagName(“H1”)[0].greenify()

试验

h1
元素不从
文档继承。原型
。他们继承了:

  • HTMLHeadingElement.prototype
  • HTMLElement.prototype
  • Element.prototype
  • Node.prototype
  • EventTarget.prototype
  • Object.prototype
例如,您可以将该方法添加到
HTMLElement.prototype

HTMLElement.prototype.greenify=function(){
this.style.color=“绿色”;
};
document.getElementsByTagName(“H1”)[0].greenify();
HTMLElement.prototype.greenify=function(){
this.style.color=“绿色”;
};
document.getElementsByTagName(“H1”)[0].greenify()

试验

h1
元素不从
文档继承。原型
。他们继承了:

  • HTMLHeadingElement.prototype
  • HTMLElement.prototype
  • Element.prototype
  • Node.prototype
  • EventTarget.prototype
  • Object.prototype
例如,您可以将该方法添加到
HTMLElement.prototype

HTMLElement.prototype.greenify=function(){
this.style.color=“绿色”;
};
document.getElementsByTagName(“H1”)[0].greenify();
HTMLElement.prototype.greenify=function(){
this.style.color=“绿色”;
};
document.getElementsByTagName(“H1”)[0].greenify()

试验

你说的“点函数”是指一种方法吗?您需要将其添加到要调用它的对象的原型中,但添加到其他人的原型(甚至是浏览器的原型)是个坏主意。。。。请不要这样做。添加一个函数
greenify()
,它接受一个HTML元素,然后改为这样做。尽管可以做到这一点,但我完全同意Matt的观点。当然这是意见的问题,但看看这篇文章:。。我宁愿建议你自己制作一个小“对象”来处理这些事情。@briosheje这不是一个观点,它实际上是危险的,因为变量或库可以设置为无冲突选项,但如果你扩展某人的原型,这是不实际的。根据实现的不同,您可能还会在本机对象周围遇到不寻常的原型。您所说的“点函数”是指方法吗?您需要将其添加到要调用它的对象的原型中,但添加到其他人的原型(甚至是浏览器的原型)是个坏主意。。。。请不要这样做。添加一个函数
greenify()
,它接受一个HTML元素,然后改为这样做。尽管可以做到这一点,但我完全同意Matt的观点。当然这是意见的问题,但看看这篇文章:。。我宁愿建议你自己制作一个小“对象”来处理这些事情。@briosheje这不是一个观点,它实际上是危险的,因为变量或库可以设置为无冲突选项,但如果你扩展某人的原型,这是不实际的。根据实现的不同,您可能还会在本机对象周围遇到不寻常的原型。请注意,添加到不受您控制的原型(尤其是本机原型)是一种不好的做法,可能会导致与其他库发生冲突。你应该尽可能避免。但是,这个答案是正确的,因此没有dv。非常感谢,我从本周开始就已经尝试过了。:)请注意,添加到不受您控制的原型(尤其是本机原型)是一种不好的做法,可能会导致与其他库发生冲突。你应该尽可能避免。但是,这个答案是正确的,因此没有dv。非常感谢,我从本周开始就已经尝试过了。:)