var start=Date.now();
document.write("<h1>This is a paragraph.</h1>");
function myFunction()
{
x=document.getElementById("demo");
x.innerHTML="Hello JavaScript! by id";
}
function myFun()
{
x=document.getElementsByClassName("demo");
x[0].innerHTML="Hello JavaScript! by class";
}
function changeImg()
{
x=document.getElementById(‘lamp‘);
if(x.src.match("../resources/img/on.gif")){
x.src="../resources/img/off.gif";}
else{
x.src="../resources/img/on.gif";
}
}
function red(){
x=document.getElementById(‘red‘);
x.style.color="#ff0000";
}
function submit(){
x=document.getElementById(‘num‘).value;
if(x==""||isNaN(x)){
alert("not number");
}else{
alert("is number");
}
}
document.write("Hello \
world!");
var x=2;
var y=x+3;
var z="Yes,I am!"
document.write(x,y,z);
var w=123e5,q=123e-5;
document.write(w+q);
var cars=new Array();
cars[0]="aaa";
cars[1]="bbb";
document.write(cars[1]);
var doors=new Array("ccc","ddd","eee");
document.write(doors[2]);
var wood=["fff","gggg","hhh"];
document.write(wood[0]); //b,e,f
var person={
first:"Bill",
last:"Gates",
age:"12"
}
var x;
var txt="";
document.write("<br/>"+person.first+"<br/>");
document.write(person["last"]+"<br/>");
for(x in person){
txt+=person[x];
}
document.write(txt);
myName=new Object();
myName.first="li";
myName.last="jin";
myName.age=19;
document.write("<br/>"+myName.first+" is "+myName.age+" years old. jin的字符串长度是"+myName.last.length+"转换成大写"+myName.first.toUpperCase()+myName.last.toUpperCase());
function add(name,job){
alert("Welcome "+name+" ,the "+job);
}
function cat(a,b){
return a*b;
}
document.getElementById("logic").innerHTML=cat(3,5);
var txt1="<br/>What a very"
var txt2="nice day!"
var txt3=txt1+" "+txt2;
document.write(txt3);
x=5+5;
document.write(x);
document.write("<br />"); //10
x="5"+"5";
document.write(x);
document.write("<br />"); //55
x=5+"5";
document.write(x);
document.write("<br />");//55
x="5"+5;
document.write(x);
document.write("<br />");//55
//星期几
function week(){
var x;
var d=new Date().getDay();
switch(d){
case 0:x="Today is Sunday.";break;
case 1:x="Today is Mon.";break;
case 2:x="Today is Tues.";break;
case 3:x="Today is Wed.";break;
case 4:x="Today is Thur.";break;
case 5:x="Today is Fri.";break;
case 6:x="Today is Sat.";break;
}
document.getElementById(‘weeks‘).innerHTML=x;
}
cars=["BWM","VOLVE","SAAB","FORD"];
var i=0;
while(cars[i]){
document.write(cars[i]);
i++;
}
document.write("<br/>");
var j=0;
for(;cars[j];){
document.write(cars[j]);
j++;
}
function some(){
try{
adddlert("Welcome guest!");
}
catch(a){
txt="页面有一个错误\n\n"+"错误描述:"+a.message+"\n\n"+"点击继续。"
alert(txt);
}
}
function test(){
try{
var x=document.getElementById(‘write‘).value;
if(x=="") throw "值为空";
if (isNaN(x)) throw "不是数字";
if (x>10) throw "太大";
if(x<5) throw "太小";
}
catch(err){
var y=document.getElementById(‘test‘);
y.innerHTML="错误:"+err+"。";
}
}
//表单验证 必填(或必选)项目
function validate_required(field,alerttxt){
with(field)
{
if(value==null||value==""){
alert(alerttxt);
return false;
}
else{
return true;
}
}
}
function validate_form(thisform){
with(thisform){
if(validate_required(email,"Email must be filled out!")==false){
email.focus();
return false;
}
}
}
//邮箱验证
function validate_emaile(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false}
else {return true}
}
}
function validate_forme(thisform)
{
with (thisform)
{
if (validate_emaile(email,"Not a valid e-mail address!")==false)
{email.focus();return false}
}
}
//DOM
var x=document.getElementById("main");
var y=x.getElementsByTagName(‘p‘);
document.write(y[0].innerHTML);
y[1].innerHTML="改变了内容哦"
//日期
document.write("<br/>今天的日期是:"+Date());
//css
document.getElementById(‘p2‘).style.color="red";
function change(id){
id.innerHTML="欢迎!"
}
document.getElementById(‘foc‘).onclick=function(){displayDate()};
function displayDate(){
document.getElementById(‘em‘).innerHTML=Date();
}
function aler(){
alert("欢迎进入(^.^)");
}
var message = "some String";
document.write(typeof bcd);
document.write(typeof message);
document.write(typeof(null));
document.write(typeof 32);
// var color=‘blue‘;
// function changeColor(){
// if (color===‘blue‘) {
// color="red";
// }else{
// color=‘red‘;
// };
// }
// changeColor();
// document.write("<br/>"+color);
var color="blue";
function changeColor(){
var anotherColor="red";
function swapColor(){
var tempColor= anotherColor;
anotherColor =color;
color=tempColor;
alert(color);
}
swapColor();
}
changeColor();
var personsb={
name: "koooh",
age: 12,
5:true
}
document.write("<p>"+personsb["name"]+"</p>");
document.write("<p>"+personsb.name+"</p>");
document.write("<p>"+personsb[5]+"</p>");
//数组Array
var computers=new Array(3);
var pointer=new Array("poin1","poin2","poin3");
var clothes=["red","green","blue"];
document.write("<p>"+clothes+"</p>");
document.write("<p>"+clothes.toString()+"</p>");
document.write("<p>"+clothes.valueOf()+"</p>");
document.write("<p>"+clothes.join("&&")+"</p>");
clothes[clothes.length]="black";
document.write("<p>"+clothes[3]+"</p>");
if(Array.isArray(clothes)){
document.write("hello!");
}
var person1={
toLocaleString:function(){
return "Niko";
},
toString:function(){
return "Mimo";
}
}
var person2={
toLocaleString:function(){
return "Niko2";
},
toString:function(){
return "Mimo2";
}
}
var people=[person1,person2];
document.write("<p>"+people+"</p>"); //默认字符串转换为toString()
document.write("<p>"+people.toString()+"</p>");
document.write("<p>"+people.toLocaleString()+"</p>");
var someColors=new Array();
var count=someColors.push("red","green");
document.write("<p>"+count+"</p>"); //2
document.write("<p>"+someColors+"</p>"); //red,green
count=someColors.push("black");
document.write("<p>"+count+"</p>"); //3
document.write("<p>"+someColors+"</p>"); //red,green,black
count=someColors.pop();
document.write("<p>"+count+"</p>"); //black !important
document.write("<p>"+someColors+"</p>"); //red,green
count = someColors.shift();
document.write("<p>"+count+"</p>"); //red
document.write("<p>"+someColors+"</p>"); //green
count=someColors.unshift("yellow","pink");
document.write("<p>"+count+"</p>"); //3
document.write("<p>"+someColors+"</p>"); //yellow,pink,green
var num=[1,2,3,4,5,23,10,13,23];
document.write("<p>"+num+"</p>");
document.write("<p>"+num.reverse()+"</p>"); //顺序反传
document.write("<p>"+num.sort()+"</p>"); //字符串从小到大,并不是数字从小到大
function compares(a,b){
if(a<b){
return 1; //比较函数第一个应该位于第二个之后的返回1
}
if(a>b){
return -1;
}
else{
return 0;
}
};
num.sort(compares)
document.write("<p>"+num+"</p>");
function cp(a,b){
return a-b;
}
num.sort(cp) //concat 和 slice不会改变原始数组
document.write("<p>"+num+"</p>");
var dogs=["dots","white"];
var dogs2=dogs.concat("yellow","brown","black");
document.write("<p>"+dogs+"</p>"); //dots,white
document.write("<p>"+dogs2+"</p>"); //dots,white,yellow,brown,black
var dogs3=dogs2.slice(1);
var dogs4=dogs2.slice(1,3);
document.write("<p>"+dogs3+"</p>"); //white,yellow,brown,black
document.write("<p>"+dogs4+"</p>"); //white,yellow
var numbers=[1,2,3,4,5,4,3,2,1];
var everyResult=numbers.every(function(item){
return(item>2);
})
document.write("<p>"+everyResult+"</p>");//false
var someResult=numbers.some(function(item,index,array){
return(item>2);
})
document.write("<p>"+someResult+"</p>");//true
var filterResult=numbers.filter(function(item,index,array){
return(item>2);
})
document.write("<p>"+filterResult+"</p>");//3,4,5,4,3
var mapResult=numbers.map(function(item,index,array){
return(item>2);
})
document.write("<p>"+mapResult+"</p>");//false,false,true,true,true,true,true,false,false
var mapResult=numbers.map(function(item,index,array){
return(item*2);
})
document.write("<p>"+mapResult+"</p>");//2,4,6,8,10,8,6,4,2
numbers.forEach(function(item,index,array){
document.write(item*2);//2468108642 forEach无返回值
})
var values=[1,2,3,4,5,6];
var sum=values.reduce(
function(prev,cur,index,array){
return prev+cur;
})
document.write("<p>"+sum+"</p>");
var sumr=values.reduceRight(
function(prev,cur,index,array){
return prev+cur;
})
document.write("<p>"+sumr+"</p>");
//Date
var stop=Date.now();
var time=stop-start;
document.write("<p>"+time+"</p>");
//正则表达式
var text = "000-00-0000";
var pattern = /\d{3}-\d{2}-\d{4}/;
if(pattern.test(text)){
document.write("正则表达式哦")
}
var sumi=function(num1,num2){
return num1+num2;
}
document.write(sumi(1,3));
function suma(num1){
return num1+10;
}
function addadd(a,b){
return a(b);
}
var book=addadd(suma,20);
document.write(book);
//高端函数
function createComparisonFunction(propertyName){
return function(object1,object2){
var value11=object1[propertyName];
var value22=object2[propertyName];
if(value11<value22){
return -1;
//return value11-value22;
}else if(value11>value22){
return 1;
}
else{
return 0;
}
};
}
var data = [{name:"zhang",age:12},{name:"li",age:14}];
data.sort(createComparisonFunction("name"));
document.write(data[0].name);
data.sort(createComparisonFunction("age"));
document.write(data[0].age);
//阶乘函数
function back(num){
if(num<=1){
return num;
}else{
return num*back(num-1);
}
}
document.write("<p>5的阶乘是"+back(5)+"</p>");
function back(num){
if(num<=1){
return num;
}else{
return num*arguments.callee(num-1);
}
}
document.write("<p>5的阶乘是"+back(5)+"</p>");
window.page="red";
var o = {page:"blue"};
function sayPage(){
document.write(this.page);
}
sayPage();//red
o.sayPage=sayPage;
o.sayPage();//blue
function outer(){
inner();
}
function inner(){
document.write("<p>"+inner.caller+"</p>");
document.write("<p>"+arguments.callee.caller+"</p>");
}
outer();
//apply
function summ(num1,num2){
return num1+num2;
}
function callSum1(num1,num2){
return summ.apply(this,arguments)
}
function callSum2(num1,num2){
return summ.apply(this,[num1,num2]); //20
//return summ(num1,num2); //20
}
document.write(callSum1(10,10));
document.write(callSum2(10,10));
//call
window.page="red";
var o = {page:"blue"};
function sayPage(){
document.write(this.page);
}
sayPage();//red
sayPage.call(this); //red
sayPage.call(window); //red
sayPage.call(o); //blue
// var pages=sayPage.call(o);
// pages(); //blue
var pages=sayPage.bind(o);
pages(); //blue
//基本包装类型
var s1="some text";
var s2=s1.substring(2);
s1=null;
document.write("<p>"+s1+"</p>"); //some text
document.write("<p>"+s2+"</p>"); //me text
var obj=new Object("some text");
document.write("<p>"+obj+"</p>"); //some text
document.write(obj instanceof String); //true
var look="25";
var looks=Number(look); //数字 25 Number为转型函数
document.write("<p>"+typeof looks+"</p>"); //number
//区分
var looks2=new Number(look); //构造了新函数
document.write("<p>"+typeof looks2+"</p>"); //object