js 怎么替换array的 array.prototypee

*& 方法:Array.remove(dx) 通过遍历,重构数组
*& 功能:删除数组元素.
*& 参数:dx删除元素的下标.
Array.prototype.remove=function(dx)
&&& if(isNaN(dx)||dx&this.length){}
&&& for(var i=0,n=0;i&this.i++)
&&&&&&& if(this[i]!=this[dx])
&&&&&&&&&&& this[n++]=this[i]
&&& this.length-=1
a = ['1','2','3','4','5'];
alert(&elements: &+a+&\nLength: &+a.length);
a.remove(1); //删除下标为1的元素
alert(&elements: &+a+&\nLength: &+a.length);
复制代码 代码示例:
*& 方法:Array.baoremove(dx)
*& 功能:删除数组元素.
*& 参数:dx删除元素的下标.
*& 返回:在原数组上修改数组.
* 编辑:.cn
Array.prototype.baoremove = function(dx)
&&& if(isNaN(dx)||dx&this.length){}
&&& this.splice(dx,1);
b = ['1','2','3','4','5'];
alert(&elements: &+b+&\nLength: &+b.length);
b.baoremove(1); //删除下标为1的元素
alert(&elements: &+b+&\nLength: &+b.length);您可能感兴趣的文章:js 操作array数组的方法及属性
本文总结了array数组地3个属性,length 属性、prototype 属性、constructor 属性使用,并附注数组对象地8个分类及多个方法使用,具体如下:
对象地3个属性
1、length 属性
length 属性
length属性表示数组地长度,即其中元素地个数.因为数组地索引总是由0开始,所以一个数组地上下限分别是:0和length-1.和其他大多数不同地是,javascript数组地length属性是可变地,这一点需要特别注意.当length属性被设置的更大时,整个数组地状态事实上不会发生变化,仅仅是length属性变大;当length属性被设置的比原来小时,则原先数组中索引大于或等于length地元素地值全部被丢失.下面是演示改变length属性地例子:
var arr=[12,23,5,3,25,98,76,54,56,76];//定义了一个包含10个数字地数组
alert(arr.length); //显示数组地长度10
arr.length=12; //增大数组地长度
alert(arr.length); //显示数组地长度已经变为12
alert(arr[8]); //显示第9个元素地值,为56
arr.length=5; //将数组地长度减少到5,索引等于或超过5地元素被丢弃
alert(arr[8]); //显示第9个元素已经变为undefined
arr.length=10; //将数组长度恢复为10
alert(arr[8]); //虽然长度被恢复为10,但第9个元素却无法收回,显示undefined
由上面地代码我们可以清楚地看到length属性地性质.但length对象不仅可以显式地设置,它也有可能被隐式修改.javascript中可以使用一个未声明过地变量,同样,也可以使用一个未定义地数组元素(指索引超过或等于length地元素),这时,length属性地值将被设置为所使用元素索引地值加1.例如下面地代码:
var arr=[12,23,5,3,25,98,76,54,56,76];//定义了一个包含10个数字地数组
alert(arr.length);//显示10
arr[15]=34;
alert(arr.length);//显示16
代码中同样是先定义了一个包含10个数字地数组,通过alert语句可以看出其长度为10.随后使用了索引为15地元素,将其赋值为15,即arr[15]=34,这时再用alert语句输出数组地长度,的到地是16.无论如何,对于习惯于强类型编程地开发人员来说,这是一个很令人惊讶地特性.事实上,使用new array()形式创建地数组,其初始长度就是为0,正是对其中未定义元素地操作,才使数组地长度发生变化.
由上面地介绍可以看到,length属性是如此地神奇,利用它可以方便地增加或者减少数组地容量.因此对length属性地深入了解,有助于在开发过程中灵活运用.
2、prototype 属性
prototype 属性
返回对象类型原型地引用.prototype 属性是 object 共有地.
objectname.prototype
objectname 参数是object对象地名称.
说明:用 prototype 属性提供对象地类地一组基本功能. 对象地新实例&继承&赋予该对象原型地操作.
对于数组对象,以以下例子说明prototype 属性地用途.
给数组对象添加返回数组中最大元素值地方法.要完成这一点,声明一个函数,将它加入 array.prototype, 并使用它.
function array_max( )
&& var i, max = this[0];
&& for (i = 1; i & this. i++)
&&&&&& if (max & this[i])
&&&&&& max = this[i];
array.prototype.max = array_
var x = new array(1, 2, 3, 4, 5, 6);
var y = x.max( );
该代码执行后,y 保存数组 x 中地最大值,或说 6.
3、constructor 属性
constructor 属性
表示创建对象地函数.
object.constructor //object是对象或函数地名称.
说明:constructor 属性是所有具有 prototype 地对象地成员.它们包括除 global 和 math 对象以外地所有 jscript 固有对象.constructor 属性保存了对构造特定对象实例地函数地引用.
x = new string(hi);
if (x.constructor == string) // 进行处理(条件为真).
function myfunc {
// 函数体.
if (y.constructor == myfunc) // 进行处理(条件为真).
对于数组来说:
y = new array();
数组对象地8个分类及多个方法
1.数组地创建
var arrayobj = new array(); //创建一个默认数组,长度是0
var arrayobj = new array(size); //创建一个size长度地数组,注意array地长度是可变地,所以不是上限,是长度
var arrayobj = new array(item1,item2,); //创建一个数组并赋初值
要说明地是,虽然第二种方法创建数组指定了长度,但实际上所有情况下数组都是变长地,也就是说即使指定了长度为5,仍然可以将元素存储在规定长度以外地,注意:这时长度会随之改变.
2、数组地元素地访问
var arrayitemvalue=arrayobj[1]; //获取数组地元素值
arrayobj[1]= 要赋予新值; //给数组元素赋予新地值
本文总结了array数组地3个属性,length 属性、prototype 属性、constructor 属性使用,并附注数组对象地8个分类及多个方法使用,具体如下:
对象地3个属性
1、length 属性
length 属性
length属性表示数组地长度,即其中元素地个数.因为数组地索引总是由0开始,所以一个数组地上下限分别是:0和length-1.和其他大多数语言不同地是,javascript数组地length属性是可变地,这一点需要特别注意.当length属性被设置的更大时,整个数组地状态事实上不会发生变化,仅仅是length属性变大;当length属性被设置的比原来小时,则原先数组中索引大于或等于length地元素地值全部被丢失.下面是演示改变length属性地例子:
var arr=[12,23,5,3,25,98,76,54,56,76];//定义了一个包含10个数字地数组
alert(arr.length); //显示数组地长度10
arr.length=12; //增大数组地长度
alert(arr.length); //显示数组地长度已经变为12
alert(arr[8]); //显示第9个元素地值,为56
arr.length=5; //将数组地长度减少到5,索引等于或超过5地元素被丢弃
alert(arr[8]); //显示第9个元素已经变为undefined
arr.length=10; //将数组长度恢复为10
alert(arr[8]); //虽然长度被恢复为10,但第9个元素却无法收回,显示undefined
由上面地代码我们可以清楚地看到length属性地性质.但length对象不仅可以显式地设置,它也有可能被隐式修改.javascript中可以使用一个未声明过地变量,同样,也可以使用一个未定义地数组元素(指索引超过或等于length地元素),这时,length属性地值将被设置为所使用元素索引地值加1.例如下面地代码:
var arr=[12,23,5,3,25,98,76,54,56,76];//定义了一个包含10个数字地数组
alert(arr.length);//显示10
arr[15]=34;
alert(arr.length);//显示16
代码中同样是先定义了一个包含10个数字地数组,通过alert语句可以看出其长度为10.随后使用了索引为15地元素,将其赋值为15,即arr[15]=34,这时再用alert语句输出数组地长度,的到地是16.无论如何,对于习惯于强类型编程地开发人员来说,这是一个很令人惊讶地特性.事实上,使用new array()形式创建地数组,其初始长度就是为0,正是对其中未定义元素地操作,才使数组地长度发生变化.
由上面地介绍可以看到,length属性是如此地神奇,利用它可以方便地增加或者减少数组地容量.因此对length属性地深入了解,有助于在开发过程中灵活运用.
2、prototype 属性
prototype 属性
返回对象类型原型地引用.prototype 属性是 object 共有地.
objectname.prototype
objectname 参数是object对象地名称.
说明:用 prototype 属性提供对象地类地一组基本功能. 对象地新实例&继承&赋予该对象原型地操作.
对于数组对象,以以下例子说明prototype 属性地用途.
给数组对象添加返回数组中最大元素值地方法.要完成这一点,声明一个函数,将它加入 array.prototype, 并使用它.
function array_max( )
&& var i, max = this[0];
&& for (i = 1; i & this. i++)
&&&&&& if (max & this[i])
&&&&&& max = this[i];
array.prototype.max = array_
var x = new array(1, 2, 3, 4, 5, 6);
var y = x.max( );
该代码执行后,y 保存数组 x 中地最大值,或说 6.
3、constructor 属性
constructor 属性
表示创建对象地函数.
object.constructor //object是对象或函数地名称.
说明:constructor 属性是所有具有 prototype 地对象地成员.它们包括除 global 和 math 对象以外地所有 jscript 固有对象.constructor 属性保存了对构造特定对象实例地函数地引用.
x = new string(hi);
if (x.constructor == string) // 进行处理(条件为真).
function myfunc {
// 函数体.
if (y.constructor == myfunc) // 进行处理(条件为真).
对于数组来说:
y = new array();
数组对象地8个分类及多个方法
1.数组地创建
var arrayobj = new array(); //创建一个默认数组,长度是0
var arrayobj = new array(size); //创建一个size长度地数组,注意array地长度是可变地,所以不是上限,是长度
var arrayobj = new array(item1,item2,); //创建一个数组并赋初值
要说明地是,虽然第二种方法创建数组指定了长度,但实际上所有情况下数组都是变长地,也就是说即使指定了长度为5,仍然可以将元素存储在规定长度以外地,注意:这时长度会随之改变.
2、数组地元素地访问
var arrayitemvalue=arrayobj[1]; //获取数组地元素值
arrayobj[1]= 要赋予新值; //给数组元素赋予新地值
: 更多网络编程信息请查看:js 中的this,constructor ,prototype详解
这一章我们将会重点介绍JavaScript中几个重要的属性(this、constructor、prototype), 这些属性对于我们理解如何实现JavaScript中的类和继承起着至关重要的作用。
this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window; 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用。 我们还可以使用apply和call两个全局方法来改变函数中this的具体指向。
先看一个在全局作用范围内使用this的例子:
console.log(this === window);
console.log(window.alert === this.alert);
console.log(this.parseInt("021", 10));
函数中的this是在运行时决定的,而不是函数定义时,如下:
// 定义一个全局函数
function foo() {
console.log(this.fruit);
// 定义一个全局变量,等价于window.fruit = "apple";
var fruit = "apple";
// 此时函数foo中this指向window对象
// 这种调用方式和window.foo();是完全等价的
// "apple"
// 自定义一个对象,并将此对象的属性foo指向全局函数foo
var pack = {
fruit: "orange",
// 此时函数foo中this指向window.pack对象
pack.foo(); // "orange"
全局函数apply和call可以用来改变函数中this的指向,如下:
// 定义一个全局函数
function foo() {
console.log(this.fruit);
// 定义一个全局变量
var fruit = "apple";
// 自定义一个对象
var pack = {
fruit: "orange"
// 等价于window.foo();
foo.apply(window);
// "apple"
// 此时foo中的this === pack
foo.apply(pack);
// "orange"
注:apply和call两个函数的作用相同,唯一的区别是两个函数的参数定义不同。
因为在JavaScript中函数也是对象,所以我们可以看到如下有趣的例子:
// 定义一个全局函数
function foo() {
if (this === window) {
console.log("this is window.");
// 函数foo也是对象,所以可以定义foo的属性boo为一个函数
foo.boo = function() {
if (this === foo) {
console.log("this is foo.");
} else if (this === window) {
console.log("this is window.");
// 等价于window.foo();
// this is window.
// 可以看到函数中this的指向调用函数的对象
foo.boo();
// this is foo.
// 使用apply改变函数中this的指向
foo.boo.apply(window);
// this is window.
我们已经在第一章中使用prototype模拟类和继承的实现。 prototype本质上还是一个JavaScript对象。
并且每个函数都有一个默认的prototype属性。
如果这个函数被用在创建自定义对象的场景中,我们称这个函数为构造函数。 比如下面一个简单的场景:
// 构造函数
function Person(name) {
this.name =
// 定义Person的原型,原型中的属性可以被自定义对象引用
Person.prototype = {
getName: function() {
return this.
var zhang = new Person("ZhangSan");
console.log(zhang.getName());
// "ZhangSan"
作为类比,我们考虑下JavaScript中的数据类型 - 字符串(String)、数字(Number)、数组(Array)、对象(Object)、日期(Date)等。
我们有理由相信,在JavaScript内部这些类型都是作为构造函数来实现的,比如:
// 定义数组的构造函数,作为JavaScript的一种预定义类型
function Array() {
// 初始化数组的实例
var arr1 = new Array(1, 56, 34, 12);
// 但是,我们更倾向于如下的语法定义:
var arr2 = [1, 56, 34, 12];
同时对数组操作的很多方法(比如concat、join、push)应该也是在prototype属性中定义的。
实际上,JavaScript所有的固有数据类型都具有只读的prototype属性
(这是可以理解的:因为如果修改了这些类型的prototype属性,则哪些预定义的方法就消失了),
但是我们可以向其中添加自己的扩展方法。
// 向JavaScript固有类型Array扩展一个获取最小值的方法
Array.prototype.min = function() {
var min = this[0];
for (var i = 1; i < this. i++) {
if (this[i] < min) {
min = this[i];
// 在任意Array的实例上调用min方法
console.log([1, 56, 34, 12].min());
注意:这里有一个陷阱,向Array的原型中添加扩展方法后,当使用for-in循环数组时,这个扩展方法也会被循环出来。
下面的代码说明这一点(假设已经向Array的原型中扩展了min方法):
var arr = [1, 56, 34, 12];
var total = 0;
for (var i in arr) {
total += parseInt(arr[i], 10);
console.log(total);
解决方法也很简单:
var arr = [1, 56, 34, 12];
var total = 0;
for (var i in arr) {
if (arr.hasOwnProperty(i)) {
total += parseInt(arr[i], 10);
console.log(total);
constructor
constructor始终指向创建当前对象的构造函数。比如下面例子:
// 等价于 var foo = new Array(1, 56, 34, 12);
var arr = [1, 56, 34, 12];
console.log(arr.constructor === Array); // true
// 等价于 var foo = new Function();
var Foo = function() { };
console.log(Foo.constructor === Function); // true
// 由构造函数实例化一个obj对象
var obj = new Foo();
console.log(obj.constructor === Foo); // true
// 将上面两段代码合起来,就得到下面的结论
console.log(obj.constructor.constructor === Function); // true
但是当constructor遇到prototype时,有趣的事情就发生了。
我们知道每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数。如下例所示:
function Person(name) {
this.name =
Person.prototype.getName = function() {
return this.
var p = new Person("ZhangSan");
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person); // true
// 将上两行代码合并就得到如下结果
console.log(p.constructor.prototype.constructor === Person); // true
当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖),
constructor的行为就有点奇怪了,如下示例:
function Person(name) {
this.name =
Person.prototype = {
getName: function() {
return this.
var p = new Person("ZhangSan");
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person); // false
console.log(p.constructor.prototype.constructor === Person); // false
为什么呢?
原来是因为覆盖Person.prototype时,等价于进行如下代码操作:
Person.prototype = new Object({
getName: function() {
return this.
而constructor始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object,即是:
function Person(name) {
this.name =
Person.prototype = {
getName: function() {
return this.
var p = new Person("ZhangSan");
console.log(p.constructor === Object);
console.log(Person.prototype.constructor === Object); // true
console.log(p.constructor.prototype.constructor === Object); // true
怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:
function Person(name) {
this.name =
Person.prototype = new Object({
getName: function() {
return this.
Person.prototype.constructor = P
var p = new Person("ZhangSan");
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person); // true
console.log(p.constructor.prototype.constructor === Person); // true
js 中的this,constructor ,prototype详解当前位置: →
→ JS常用方法--String.prototype使用
JS常用方法--String.prototype使用
& 作者及来源: 阳光囧男 - 博客园 &
&收藏到→_→:
摘要: JS常用方法--String.prototype使用!
"JS常用方法--String.prototype使用"::
titlenew document title
meta name="generator" content="editplus"
meta name="author" content=""
meta name="keywords" content=""
meta name="description" content=""
script language="javascript"
string.prototype.replace = function(oldvalue,newvalue)
var reg = new regexp(oldvalue,"g");
return this.replace(reg, newvalue);
function replace(obj)
alert(obj.value.replace("a","d"));
function savecode(obj, filename)
var win = window.open('', '_blank', 'top=100');
var code = obj.
code = code == null || code == "" ? obj.value :
win.opener = null;
win.document.write(code);
win.document.execcommand('saveas', true, filename);
win.close();
window.onload = function()
var now = new date();
var hour = now.gethours();
if (hour & 6)
greeting = "凌晨好";
else if (hour & 10)
greeting = "早上好";
else if (hour & 14)
greeting = "中午好";
else if (hour & 18)
greeting = "下午好";
greeting = "晚上好";
document.getelementbyid("hi").innerhtml = "&font color=red&" + greeting + "&/font&" ;
function putcursoratlast(obj)
obj.focus();
var range = obj.createtextrange();
range.movestart('character',obj.value.length);
range.collapse(true);
range.select();
function putcursoratfirst(obj)
obj.focus();
var range = obj.createtextrange();
range.movestart('character',0);
range.collapse(true);
range.select();
headbodyspan id="hi"spanbr
spancurssor at last spanbr
input type="text" value="curssor at last" onclick="putcursoratlast(this)"
spancurssor at first spanbr
input type="text" value="curssor at first" onclick="putcursoratfirst(this)"
spanstring.replace spanbr
input type="text" value="replace" onclick="replace(this)"
spansave file spanbr
input type="text" value="hello word" onclick='savecode(this,"save")'
&!doctype html public "-//w3c//dtd html 4.0 transitional//en"&
&html&&head&
&title&new document &/title&
&meta name="generator" content="editplus"&
&meta name="author" content=""&
&meta name="keywords" content=""&
&meta name="description" content=""&
&script language="javascript"&
string.prototype.replace = function(oldvalue,newvalue)
var reg = new regexp(oldvalue,"g");
return this.replace(reg, newvalue);
//字符串替换;曾经很头疼写了很多代码,还是这个简单
function replace(obj)
alert(obj.value.replace("a","d"));
// 另存为文件
function savecode(obj, filename)
var win = window.open('', '_blank', 'top=100');
var code = obj.
code = code == null || code == "" ? obj.value :
win.opener =
win.document.write(code);
win.document.execcommand('saveas', true, filename);
win.close();
window.onload = function()
var now = new date();
var hour = now.gethours();
if (hour & 6)
greeting = "凌晨好";
else if (hour & 10)
greeting = "早上好";
else if (hour & 14)
greeting = "中午好";
else if (hour & 18)
greeting = "下午好";
greeting = "晚上好";
document.getelementbyid("hi").innerhtml = "&font color=red&" + greeting + "&/font&" ;
// 将光标停在对象的最后
function putcursoratlast(obj)
obj.focus();
var range = obj.createtextrange();
range.movestart('character',obj.value.length);
range.collapse(true);
range.select();
// 将光标停在对象的最前
function putcursoratfirst(obj)
obj.focus();
var range = obj.createtextrange();
range.movestart('character',0);
range.collapse(true);
range.select();
&/head&&body&&span id="hi"&&/span&&br /&
&span&curssor at last &/span&&br /&
&input type="text" value="curssor at last" onclick="putcursoratlast(this)"&
&span&curssor at first &/span&&br /&
&input type="text" value="curssor at first" onclick="putcursoratfirst(this)"&
&span&string.replace &/span&&br /&
&input type="text" value="replace" onclick="replace(this)"&
&span&save file &/span&&br /&
&input type="text" value="hello word" onclick='savecode(this,"save")' /&
&/body&&/html&
复制javascript代码保存代码
string.prototype.chineselength=function()
return this.replace(/[^\x00-\xff]/g,"**").
string.prototype.endswith = function(str)
return this.substr(this.length - str.length) ==
string.prototype.lefttrim = function()
return this.replace(/(^[\\s]*)/g, "");
string.prototype.righttrim = function()
return this.replace(/([\\s]*$)/g, "");
string.prototype.startswith = function(str)
return this.substr(0, str.length) ==
string.prototype.trim = function()
return this.replace(/(^\s*)|(\s*$)/g, "");
}都是基于&string.prototype&的扩展: 起因是有个网友和我讨论两个函数, 一个是&isdatetime&(判断字符是否是符合&yyyy-mm-dd&hh:mm:ss日期格式) 另一个是&left&函数,类似vbscript的left&实现中英文字符的混合截取。 他两个函数都用了循环,还用了n多&if&语句,每个函数都超此文来自: 马开东博客
转载请注明出处 网址:
过了40行代码,问我有无好的办法精简一下。 于是,我就写出了下面的代码,不敢说最效率最高,但是已经是够精简了,&left函数才1行
阅读代码编辑代码运行效果复制html代码保存代码
script type="text/javascript"
function $a(arraylike){
for(var i=0,ret=[];i&arraylike.i++) ret.push(arraylike[i])
return ret
array.prototype.any=function(f){
for(var i=0;i&this.i++) if (f(this[i],i,this)) return true;
return false
string.prototype.isdatetime=function(){
var arr=(this.length==19)?this.split(/\d/):[]
eval("var d=new date("+arr.join(",")+")")
number(arr[0])==d.getfullyear() && number(arr[1])==d.getmonth()
&& number(arr[2])==d.getdate() && number(arr[3])==d.gethours()
&& number(arr[4])==d.getminutes() && number(arr[5])==d.getseconds()
}catch(x){return false}
string.prototype.startswith=function(){
var _string=this
return $a(arguments).any(function(value){return _string.slice(0,value.length)==value})
string.prototype.endswith=function(){
var _string=this
return $a(arguments).any(function(value){return _string.slice(value.length*(-1))==value})
string.prototype.left=function(n){
return this.slice(0,n-this.slice(0,n).replace(/[\x00-\xff]/g,"").length)
string.prototype.right=function(n){
return this.slice(this.slice(-n).replace(/[\x00-\xff]/g,"").length-n)
script搜索此文相关文章:常用方法--String.prototype使用此文来自: 马开东博客
网址: 站长QQ
JS常用方法--String.prototype使用_博客园相关文章
博客园_总排行榜
博客园_最新
博客园_月排行榜
博客园_周排行榜
博客园_日排行榜

我要回帖

更多关于 array.prototype.push 的文章

 

随机推荐