</pre><pre name="code" class="javascript">if(true){
var color="blue";
}
alert(color);//是否存在? 答案是 blue
//使用var 声明的变量会自动被添加到最近的环境(作用域)中,
function add(num1,num2){
var result=num1+num2;
re=result;
return result;
}
alert(add(1,1.2));
//if(result instanceof undefined )alert (""+result); 此处代码有问题
alert(typeof result);
alert(re);
var person=new Object(); /**直接new**/
person.name="kebe"; //为 person变量动态生成 属性;
alert(person.name); //访问对象的属性 一般使用点(.)
alert(person["name"]); //也可以用方括号([])来访问 但是方括号内的内容 必须是字符串的形式
debugger;
var str="";
str.name="nikolas"; //无法为基本类型动态生成属性
alert(str.name);
alert (name);
var student ={ /**对象字面量**/
name:'nicholas',
age:12,
//sex:male 此条语句出错 因为 冒号(:)之后必须是 非undefined
sex:"male"
};
alert("student.name="+student.name+",student.age="+student.age+",student.sex="+student.sex);
/*
*
*数组操作
*/
var persons=new Array();
var values=Array();
optionsValue= [];
var count=persons.push("red","black"); // push将指定的元素压入栈栈中 然后返回数组或者说栈的长度
var p=persons.pop();// pop 取数组的最后一个元素,length-1; 出栈方式
var p1=persons.shift();//shift 取数组的第一个元素 ,length-1; 出对列的方式
persons.reverse();//将数组倒序
persons.sort();//将数组排序
/**
* 0 升序 ,-1 倒序, 1 正序
*/
function comparetor(value1,value2){
return value1-value2;
}
persons.sort(compatetor);