简体   繁体   English

(覆盖||扩展)Javascript方法

[英](Override || Extend) Javascript Method

I want to change the method DomElement.appendChild() to work differently when applied on an Object that I have created: 我想将方法​​DomElement.appendChild()更改为在应用到我创建的对象上时以不同的方式工作:

// using MooTools;
var domElement = new Class({...});

var MyObject = new domElement(...);

$(document).appendChild(MyObject); // Error

// I want to add to (extend) appendChild, to do something specific, when:
alert(MyObject instanceof domElement); // true

I could do this by modifying the mootools.js, but I want to avoid this at all costs, because I am certain one day, some other developer will overwrite the js file with an updated version, and I will be imprisoned for murder. 我可以通过修改mootools.js来做到这一点,但是我想不惜一切代价避免这种情况,因为我确定有一天,其他一些开发人员将用更新的版本覆盖js文件,而我将因谋杀而入狱。

Please, keep me out of jail! 求你了,别让我出狱!

I dont know about MooTools, but there is a simple way in native JS to do this.. 我不了解MooTools,但是在本机JS中有一种简单的方法可以做到这一点。

    var orgAppendChild = document.appendChild;  
    document.appendChild = localAppendHandler;


    var domElement = new Class({...});
    var MyObject = new domElement(...);
    // document.appendChild(MyObject);

     var ss = document.createElement('tr'); 
document.appendChild (ss);


    function localAppendHandler(MyObject)
    {
     if (MyObject instanceof domElement)
        UrCustomRoutine(MyObject )
        else
            orgAppendChild(MyObject);


    }

    function UrCustomRoutine(MyObject ){
    //your custom routine
    }

Hope this helps 希望这可以帮助

Update: From your further explanation (handling appendChild of any DOM element), i understand that there is no generic way of defining local hanlders to trap the event. 更新:从您的进一步说明(处理任何DOM元素的appendChild),我知道没有定义本地处理程序来捕获事件的通用方法。

But there is a workaround (this is very ugly i know). 但是有一个解决方法(我知道这很丑)。 Means you have to define a LocalHandler for the DOM element before you are goin to use the appendChild . 意味着您必须先使用DOM元素定义LocalHandler才能使用appendChild。

Before doing this document.getElementByID('divTarget').appendChild() 在执行此文档之前.getElementByID('divTarget')。appendChild()

you have to reassign the localhandler to the element you need to access 您必须将localhandler重新分配给您需要访问的元素

    orgAppendChild = document.getElementById('divTarget').appendChild;  
    document.getElementById('divTarget').appendChild = localAppendHandler;
    var sss = document.createElement('table'); 
    document.getElementById('divTarget').appendChild(sss);

Another Alternative: If you wanted to keep it simple, i suggest this alternate way. 另一种选择:如果您想保持简单,我建议您使用另一种方法。 Create AppendChild local method which accepts object and the node name as param. 创建AppendChild本地方法,该方法接受对象和节点名称作为参数。

    function AppendChild(MyObject ,Node)
    {
           if (MyObject instanceof domElement)
               //your custom routine
           else if (Node=="" && (MyObject instanceof domElement==false))
               document.appendChild(MyObject);
           else if (Node!="" && (MyObject instanceof domElement==false))
               eval( "document.getElementById('" + Node + "').appendChild(MyObject);");


    }

And

  1. If you wanted to append DOM element to document 如果您想将DOM元素附加到文档中

      AppendChild(DOMelement,"") 
  2. If you wanted to append DOM element inside other container 如果要在其他容器中附加DOM元素

      AppendChild(DOMelement,"divTarget") 
  3. If you wanted to process your custom object 如果要处理自定义对象

      AppendChild(customObject,"") 

this is the best way to do this. 这是最好的方法。

you ought to look at using object.toElement() in your class 您应该在类中使用object.toElement()

http://blog.kassens.net/toelement-method http://blog.kassens.net/toelement-method

then just use document.body.inject(instanceObject); 然后只需使用document.body.inject(instanceObject);

var originAppend = Element.prototype.appendChild;
Element.prototype.appendChild = function(el){ 
    // do something to el
    originAppend.call(this, el);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM