var person = new Object();
person.name = "Nicholas";
person.age = 29;
var person = {
name : "Nicholas",age : 29
};
alert(person["name"]); //"Nicholas"
alert(person.name); //"Nicholas"
var colors = new Array();//空数组
var colors = new Array(20);//创建1个包括3项的数组
var colors = new Array("red","blue","green");//创建1个包括3个字符串值的数组
var names = Array("Greg");//创建1个包括1个字符串值的数组
var colors = ["red","green"];//创建1个包括3个字符串的数组
var names = [];//创建1个空数组
var values = [1,2,];//忌讳,会创建1个包括2或3项的数组
var options = [,];//忌讳,会创建1个包括5或6项的数组
if (value instanceof Array) {
//对数组履行某些操作
}
if (Array.isArray(value)) {
//对数组履行某些操作
}
push()
: 栈中项插入pop()
:栈中项移除push()
:向数组末端添加项shift()
:从数组前段获得项function compare(value1,value2) {
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
}
var values = [0,1,5,10,15];
values.sort(compare);
alert(values); //0,15
function compare(value1,value2) {
return value2 - value1;
}
var colors = ["red","green","blue"];
var colors2 = colors.concat("yellow",["black","brown"];
alert(colors); //red,green,blue
alert(colors2); //red,blue,yellow,black,brown
var colors = ["red","yellow","purple"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);
alert(colors2); //green,purple
alert(colors3); //green,yellow
splice(0,2)
会删除数组中的前两项。splice(2,"red","green")
会从当前数组的位置2开始插入字符串”red”和”green”。splice(2,1,"green")
会删除当前数组位置2的项,然后再从当前数组的位置2开始插入字符串”red”和”green”。var colors = ["red","blue"];
var removed = colors.splice(0,1); //删除第1项
alert(colors); //green,blue
alert(removed); //red,返回的数组中只包括1项
removed = colors.splice(1,0,"orange"); //从位置1开始插入两项
alert(colors); //green,orange,blue
alert(removed); //返回的是1个空数组
removed = colors.splice(1,"red","purple"); //插入两项,删除1项
alert(colors); //green,red,purple,blue
alert(removed); //yellow,返回的数组中只包括1项
var person = { name: "Nicholas" };
var people = [{ name: "Nicholas" }];
var morePeople = [person];
alert(people.indexOf(person)); //⑴
alert(morePeople.indexOf(person)); //0
every()
:对数组中的每项运行给定函数,如果该函数对每项都返回true,则返回true。some()
:对数组中的每项运行给定函数,如果该函数对任1项返回true,则返回true。filter()
:对数组中的每项运行给定函数,返回该函数会返回true的项组成的数组。forEach()
:对数组中的每项运行给定函数。这个方法没有返回值。map()
:对数组中的每项运行给定函数,返回每次函数调用的结果组成的数组。var numbers = [1,3,4,1];
var everyResult = numbers.every(function(item,index,array) {
return (item > 2);
});
alert(everyResult); //false
var someResult = numbers.some(function(item,array) {
return (item > 2);
});
alert(someResult); //true
var numbers = [1,1];
var filterResult = numbers.filter(function(item,array) {
return (item > 2);
});
alert(filterResult); //[3,4,5,3]
var numbers = [1,1];
var mapResult = numbers.filter(function(item,array) {
return item * 2;
});
alert(mapResult); //[2,6,8,10,2]
var numbers = [1,1];
numbers.forEach(function(item,index,array) {
//履行某些操作
});
var values = [1,5];
var sum = values.reduce(function(prev,cur,array) {
return prev + cur;
});
alert(sum); //15
var now = new Date()
var someDate = new Date(Date.parse("May 25,2004"));
//GMT时间2000年1月1日午夜零时
var y2k = new Date(Date.UTC(2000,0));
//GMT时间2005年5月5日下午5:55:55
var allFives = new Date(Date.UTC(2005,17,55,55));
toDateString()
——以特定于实现的格式显示星期几、月、日和年;toTimeString()
——以特定于时间的格式显示时、分、秒和时区;toLocaleDateString()
——以特定于地区的格式显示星期几、月、日和年;toLocaleTimeString()
——以特定于实现的格式显示时、分、秒;toUTCString()
——以特定于实现的格式显示完全的UTC日期。var expression = / pattern / flags ;
//使用函数声明
alert(sum(10,10));
function sum(num1,num2) {
return num1 + num2;
}
//使用函数表达式
alert(sum(10,10));
var sum = function(num1,num2) {
return num1 + num2;
}
//函数声明
function addSomeNumber(num) {
return num + 100;
}
function addSomeNumber(num) {
return num + 200;
}
var result = addSomeNumber(100); //300
var addSomeNumber = function (num) {
return num + 100;
};
addSomeNumber = function (num) {
return num + 200;
};
var result = addSomeNumber(100); //300
//函数作为参数
function callSomeFunction (someFunction,someArgument) {
return somFunction (someArgument);
}
function add10 (num) {
return num + 10;
}
var result1 = callSomeFunction (add10,10);
alert(result1); //20
function getGreeting (name) {
return "Hello," + name;
}
var result2 = callSomeFunction (getGreeting,"Nicholas");
alert(result2); //"Hello,Nicholas"
function createComparisonFunction (propertyName) {
return function (object1,object2) {
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
};
}
var data = [{name: "Zachary",age: 28},{name: "Nicholas",age: 29}];
data.sort(createComparisonFunction("name"));
alert(data[0].name); //Nicholas
data.sort(createComparisonFunction("age"));
alert(data[0].name); //Zachary
//与函数名牢牢耦合
function factorial (num) {
if (num <= 1) {
return 1;
} else {
return num * factorial (num - 1)
}
}
//使用arguments.callee替换函数名,消除耦合
function factorial (num) {
if (num <= 1) {
return 1;
} else {
return num * arguments.callee (num - 1)
}
}
var trueFactorial = factorial;
factorial = function () {
return 0;
};
alert(trueFactiorial(5)); //120
alert(factorial(5)); //0
window.color = "red";
var o = { color: "blue"};
function sayColor() {
alert(this.color);
}
sayColor(); //"red"
o.sayColor = sayColor;
o.sayColor(); //"blue"
function sum(num1,num2) {
return num1 + num2;
}
function callSum1(num1,num2) {
return sum.apply(this,arguments); //传入arguments对象
}
function callSum2(num1,[num1,num2]); //传入数组
}
alert(callSum1(10,10)); //20
alert(callSum2(10,10)); //20
function sum(num1,num2) {
return num1 + num2;
}
function callSum(num1,num2) {
return sum.call(this,num1,num2);
}
alert(callSum(10,10)); //20
window.color = "red";
var o = { color: "blue" };
function sayColor() {
alert(this.color);
}
sayColor(); //red
sayColor.call(this); //red
sayColor.call(window); //red
sayColor.call(o); //blue
window.color = "red";
var o = { color: "blue" };
function sayColor() {
alert(this.color);
}
var objectSayColor = sayColor.bind(o);
objectSayColor(); //blue
var s1 = "some text";
var s2 = s1.substring(2);
String
类型的1个实例;var s1 = new String("some text");
var s2 = s1.substring(2);
s1 = null;
var StringObject = "hello world";
alert(StringObject .slice(3)); //输出 "lo world"
alert(StringObject .substring(3)); //输出 "lo world"
alert(StringObject .substr(3)); //输出 "lo world"
alert(StringObject .slice(3,7)); //输出 "lo w"
alert(StringObject .substring(3,7)); //输出 "lo w"
alert(StringObject .substr(3,7)); //输出 "lo worl"
var StringObject = "hello world";
alert(StringObject .slice(-3)); //输出 "rld"
alert(StringObject .substring(-3)); //输出 "hello world"
alert(StringObject .substr(-3)); //输出 "rld"
alert(StringObject .slice(3,-4)); //输出 "lo w"
alert(StringObject .substring(3,-4)); //输出 "hel"
alert(StringObject .substr(3,-4)); //输出 "" 空字符串
var stringValue = "Lorem ipsum dolor sit amet,consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");
while (pos > -1) {
positions.push(pos);
pos = stringValue.indexOf("e",pos + 1);
}
alert(positions); //"3,24,32,35,52"
var text = "cat,bat,sat,fat";
var pattern = /.at/;
//与pattern.exec(text)相同
var matches = text.match(pattern);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern.lastIndex); //0
var text = "cat,fat";
var pos = text.search(/at/);
alert(pos); //1
var text = "cat,fat";
var result = text.replace("at","ond");
alert(result); //"cond,sond,fond"
字符序列 | 替换文本 |
---|---|
$$ | $ |
$& | 匹配全部模式的子字符串。与RegExp.lastMatch的值相同 |
$’ | 匹配的子字符串之前的子字符串。与RegExp.leftContext的值相同 |
$` | 匹配的子字符串之前的子字符串。与RegExp.rightContext的值相同 |
$n | 匹配第n个捕获组的子字符组,其中nn等于0~9。例如,$1是匹配第1个捕获组的子字符串,$2是匹配第2个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串 |
$nn | 匹配第nn个捕获组的子字符串,其中nn等于01~99。例如$01是匹配第1个捕获组的子字符串,$02是匹配第2个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串 |
var text = "cat,fat";
result = text.replace(/(.at)/g,"word ($1)");
alert(result); //word (cat),word (bat),word(sat),word (fat)
\\返回html实体
function htmlEscape(text) {
return text.replace(/[<>"&]/g,function(match,pos,originalText) {
switch(match) {
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case "\"":
return """
}
});
}
alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
//<p class="greeting">Hello world!</p>
var colorText = "red,yellow";
var colors1 = colorText.split(","); //["red","yellow"]
var colors2 = colorText.split(",",2); //["red","blue"]
var colors3 = colorText.split(/[^\,]+/); //["",",""]
var stringValue = "yellow";
alert(stringValue.localeCompare("brick")); //1
alert(stringValue.localeCompare(
本站采用系统自动发货方式,付款后即出现下载入口,如有疑问请咨询在线客服!
售后时间:早10点 - 晚11:30点Copyright © 2024 jiecseo.com All rights reserved. 粤ICP备18085929号
欢迎光临【捷杰建站】,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!
技术营运:深圳市晟艺互动传媒有限公司