JS方法封装

1.封装查找子节点child方法

    Element.prototype.myChildren=function () {
        var child=this.childNodes;
        var len=child.length;
        var arr=[];
        for(var i=0;i

2.封装insertAfter方法

 Element.prototype.insertAfter=function (targetNode,afterNode) {
        var berforNode=afterNode.nextElementSibling;
        if(berforNode==null){
            this.appendChild(targetNode);
        }
        else
        {
            this.insertBefore(targetNode,berforNode);
        }

    }

3.封装typeof方法

 function type(target) {
                var obj={
                    "[object Array]":"array",
                    "[object Object]":"object",
                    "[object Number]":"number-object",
                    "[object Boolean]":"boolean-object",
                    "[object String]":"string-object"
                }
            if(target===null)
            {
                return "null";
            }
            if(typeof (target)=="object"){//引用值
                //数组 对象 包装类
                var str=Object.prototype.toString.call(target);
                return obj[str];
            }
            else {//对象值
                return typeof (target);
            }
        }

你可能感兴趣的:(JS方法封装)