首页 > 代码库 > JavaScript学习笔记(一)

JavaScript学习笔记(一)

//==="2个= 与3个= "===	var result1=("55"==55); //-> true	var result2=("55"===55); //->false	alert(result1+" ; "+result2);//=====for in======for(var propName in window){	document.write(propName);	document.write("</br>");}//====函数的return语句=====function sum(a,b){	return a+b;	alert("hello"); //return之后的语句将不会被执行}document.write(sum(2,3));function sayHi(name,message){	return; //输出undefined	alert("Heloo"); //同样Heloo也不会输出}document.write(sayHi("Me"));document.write("<br/>");function sayHi(){	document.write("Hello "+arguments[0]+","+arguments[1]);}sayHi("he","said");//========arguments与参数=====function doAdd(num1,num2){	arguments[1]=10;	alert(arguments[0]+num2);}doAdd(11,2); //-> 21doAdd(11); //->NaN,只传入了一个参数,arguments[1]设置的值不会反应到命名参数中。doAdd();   //->NaN,没有传递值的命名参数,将自动被赋予undefined值。//====没有return语句的函数======/*function add(a,b){	var result = a+b;}alert(add(2,3)); //->undefined,未指定返回值的函数返回的是一个特殊的undefined值。*///======Object======/*var person = new Object();person.name = "Nicolas";alert(person.name); //->Nicolas;var name="Nicolas";name.age = 27;alert(name.age); //->undefined,不能给基本类型的值添加属性。*///========基本类型与引用类型======function addTen(num){	num += 10;	return num;}var count = 20;var result = addTen(count);alert(count); //->20alert(result); //->30function setName(obj){	obj.name = "Nicolas";}var person = new Object();setName(person);document.write(person.name); //->Nicolasdocument.write("<br/>");function setName1(obj){	obj.name = "Angle";	obj = new Object();	obj.name = "Greg";}var person1 = new Object();setName1(person1);document.write(person1.name); //->Angle ,说明JavaScript对象参数是值传递alert(person1 instanceof Object);//========JSON========/* * JSON.stringify()把一个JavaScript对象序列化成一个JSON字符串 * 其中值为undefined的任何属性也都会被跳过 *  * */var book = {	title:"Professional JavaScript",	authors:[		"Nicholas c. Zakas"	],	edition:3,	year:2011,	denote:undefined};var jsonText = JSON.stringify(book);alert(jsonText);var copyBook = JSON.parse(jsonText);alert(copyBook["year"]);/* * 过滤结果 * JSON.stringfy(Array,["param1","param2"]) */var jsonText1 = JSON.stringify(book,["title","edition"]);document.write(jsonText1);/* * 字符串缩进 *JSON.stringify(value [, replacer] [, space])  *//*var jsonText2 = JSON.stringify(book,null,4);//缩进4个空格alert(jsonText2);/* * toJson */ var book1 = {	title:"Professional JavaScript",	authors:[		"Nicholas c. Zakas"	],	edition:3,	year:2011,	toJSON:function(){		return this;	}};var jsonText3 = JSON.stringify(book1);alert(jsonText3);

  

JavaScript学习笔记(一)