Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.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 您最喜欢的Mootools/Prototype原生对象原型是什么?_Javascript_Mootools_Prototypejs - Fatal编程技术网

Javascript 您最喜欢的Mootools/Prototype原生对象原型是什么?

Javascript 您最喜欢的Mootools/Prototype原生对象原型是什么?,javascript,mootools,prototypejs,Javascript,Mootools,Prototypejs,我们的mootooler和prototyper(本网站上很少有)通常会随身携带一个方便的工具箱,其中包含我们在本地javascript对象上创建(或借用)的函数,以使我们的生活更轻松。我想得到一个非常有用的原型函数列表,但只包括在本机对象上实现的函数(即String.implement({…,在mootools中) 那么,你最喜欢什么 PS:我包括了mootools和prototype,因为为一个库编写的函数很容易移植到另一个库 PPS:我知道支持/反对原型化原生javascript对象的论点

我们的mootooler和prototyper(本网站上很少有)通常会随身携带一个方便的工具箱,其中包含我们在本地javascript对象上创建(或借用)的函数,以使我们的生活更轻松。我想得到一个非常有用的原型函数列表,但只包括在本机对象上实现的函数(即
String.implement({…
,在mootools中)

那么,你最喜欢什么


PS:我包括了mootools和prototype,因为为一个库编写的函数很容易移植到另一个库


PPS:我知道支持/反对原型化原生javascript对象的论点,我更愿意避免在这里讨论。

这里是我最喜欢的一些mootools

字符串函数

String.implement({

    //easy way to test if a string contains characters (input.value.isEmpty())
    isEmpty : function() {
        return (!this.test(/\w+/));
    },

    //add ellipses if string length > len
    ellipse : function(len) {
        return (this.length > len) ? this.substr(0, len) + "..." : this;
    },

    //finds all indexOf occurrences
    indexesOf : function(val) {
        var from = 0;
        var indexes = [];
        while (0 <= from && from < this.length) {
            var idx = this.indexOf(val, from);
            if (idx >= 0) {
                indexes.push(idx);
            } else {
                break;
            }
            from = idx+1;
        }
        return indexes;
    }
});
Array.implement({

    //compare two arrays to see if they are identical
    compare : function(arr, strict) {
        strict = strict || false;
        if (this.length != arr.length)          return false;

        for (var i = 0; i < this.length; i++) {
            if ($type(this[i]) == "array") {
                if (!this[i].compare(arr[i]))   return false;
            }
            if (strict) {
                if (this[i] !== arr[i])     return false;
            } else {
                if (this[i] != arr[i])      return false;
            }
        }
        return true;
    },

    //remove non-unique array values
    unique : function() {
        for(var i = 0; i< this.length; i++) {
            var keys = this.indexesOf(this[i]);
            while (keys.length > 1) {
                this.splice(keys.pop(), 1);
            }
        }
        return this;
    },

    //same as array.unshift, except returns array instead of count
    //good for using inline... array.lpush('value').doSomethingElse()
    lpush : function() {
        for (var i = arguments.length -1 ; i >= 0; i--){
            this.unshift(arguments[i]);
        }
        return this;
    },

    //get all indexes of an item in an array
    indexesOf : function(item) {
        var ret = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i] == item)    ret.push(i);
        }
        return ret;
    }
});
String.implement({
//测试字符串是否包含字符的简单方法(input.value.isEmpty())
isEmpty:function(){
返回(!this.test(/\w+/);
},
//如果字符串长度>len,则添加省略号
椭圆:函数(len){
返回(this.length>len)?this.substr(0,len)+“…”:this;
},
//查找所有出现的索引
索引:函数(val){
var-from=0;
var指数=[];
而(0=0){
索引推送(idx);
}否则{
打破
}
from=idx+1;
}
收益指标;
}
});
数组函数

String.implement({

    //easy way to test if a string contains characters (input.value.isEmpty())
    isEmpty : function() {
        return (!this.test(/\w+/));
    },

    //add ellipses if string length > len
    ellipse : function(len) {
        return (this.length > len) ? this.substr(0, len) + "..." : this;
    },

    //finds all indexOf occurrences
    indexesOf : function(val) {
        var from = 0;
        var indexes = [];
        while (0 <= from && from < this.length) {
            var idx = this.indexOf(val, from);
            if (idx >= 0) {
                indexes.push(idx);
            } else {
                break;
            }
            from = idx+1;
        }
        return indexes;
    }
});
Array.implement({

    //compare two arrays to see if they are identical
    compare : function(arr, strict) {
        strict = strict || false;
        if (this.length != arr.length)          return false;

        for (var i = 0; i < this.length; i++) {
            if ($type(this[i]) == "array") {
                if (!this[i].compare(arr[i]))   return false;
            }
            if (strict) {
                if (this[i] !== arr[i])     return false;
            } else {
                if (this[i] != arr[i])      return false;
            }
        }
        return true;
    },

    //remove non-unique array values
    unique : function() {
        for(var i = 0; i< this.length; i++) {
            var keys = this.indexesOf(this[i]);
            while (keys.length > 1) {
                this.splice(keys.pop(), 1);
            }
        }
        return this;
    },

    //same as array.unshift, except returns array instead of count
    //good for using inline... array.lpush('value').doSomethingElse()
    lpush : function() {
        for (var i = arguments.length -1 ; i >= 0; i--){
            this.unshift(arguments[i]);
        }
        return this;
    },

    //get all indexes of an item in an array
    indexesOf : function(item) {
        var ret = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i] == item)    ret.push(i);
        }
        return ret;
    }
});
Array.implement({
//比较两个数组,看它们是否相同
比较:功能(arr,严格){
严格=严格| |假;
如果(this.length!=arr.length)返回false;
for(var i=0;i1){
这个.splice(key.pop(),1);
}
}
归还这个;
},
//与array.unshift相同,只是返回数组而不是计数
//适合使用内联…数组。lpush('value')。doSomethingElse()
lpush:function(){
对于(var i=arguments.length-1;i>=0;i--){
这个.unshift(参数[i]);
}
归还这个;
},
//获取数组中某项的所有索引
指标:功能(项目){
var-ret=[];
for(var i=0;i
//取自http://prototype.lighthouseapp.com/projects/8886/tickets/351-new-swap-method-for-elements
Element.addMethods({
交换:(函数(){
if(document.documentElement中的('swapNode'))
返回函数(元素、其他){
返回$(元素).swapNode($(其他));
};
返回函数(元素、其他){
元素=$(元素);
其他=$(其他);
var next=other.nextSibling,parent=other.parentNode;
元素.parentNode.replaceChild(其他,元素);
返回parent.insertBefore(元素,下一个);
};
})()
});
//扩展数组对象以支持索引插入
//提交于http://prototype.lighthouseapp.com/projects/8886-prototype/tickets/356-arrayinsert
Array.prototype.insert=函数(元素,其中){
var slice1=this.slice(0,其中);
var slice2=this.slice(其中);
返回新的Array.concat(切片1,元素,切片2);
};
//扩展数组对象以支持搜索索引数组
//如果returnIndex为true,则返回keyName,否则返回该单元格中的值
Array.prototype.nextValue=函数(startIndex,returnIndex){
对于(变量i=startIndex+1;i=0;i--){
如果(本[i]){
返回(返回索引?i:本[i]);
}
}
返回null;
};

我没有真正使用Prototype和Mootools进行开发,但我想以下内容在这些框架中也会很有用

替换本机Math.round(),该参数采用指定精度的可选第二个参数:

Math.round(3.1415, 2); // 3.14
not()函数获取否定谓词的方法:

var even = function(x){ return x % 2 === 0; };
var odd = even.not();
even(2); // true
odd(2); // false
但最有用的东西是我将添加到Object.prototype中的东西,如果这是一种安全的方法,那么我将使用一些全局函数来迭代对象属性

if(!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(){ ... };
}
objMap()与Array.map()类似,但用于对象:

// returns {a:2, b:4, c:6}
objMap({a:1, b:2, c:3}, function(value) {
  return value*2;
});
objValues()objKeys()要从对象获取属性名称或值数组:

objValues({a:1, b:2, c:3}); // [1, 2, 3]
objKeys({a:1, b:2, c:3}); // ["a", "b", "c"]
当然,objReduce()几乎可以做任何可以想象的事情


实现细节留给读者练习:-)

我喜欢在创建之前检查属性的方式,以避免覆盖本机属性

if(!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(){ ... };
}

我继续讲述tj111开始的内容,下面是我的小补充:

Array.implement({
    //calculate the sum of all integers
    sum: function() {
        var sum = this.reduce(function(a, b) {
            return a + b;
        });
        return sum;
    }
});

最后一些摩尔人!objMap、objvalue和objKeys都是在MooTools中实现的(通过Hash类)。