Q1:数据类型有哪些?
5种简单数据类型:undefined,null,boolean,number,string;
1种复杂数据类型:object。
Undefined只有一个值:undefined;
Null只有一个值:null;(表示一个空对象指针)
Boolean有两个字面值:true,false;(区分大小写,True和False等都不是boolean值,只是标识符)
Number值:整数,浮点数,十进制,八进制(第一位必须是0,严格模式下无效),十六进制(前两位必须是0x),数值范围(大多数浏览器Number.MIN_VALUE:5e-324;Number.MAX_VALUE:1.7976931348623157e+308,可以用isFinite(number)判断是否超范围,超范围后正数自动转换成Infinity值,负数为-Infinity);NaN(not a number);数值转换:Number(“123”)。。。;parseInt(“123blue”);parseInt(“AF”,16),第二个参数为转换时使用的多少进制;parseFloat(“0xA”);parseFloat(“3.125e7”);
String类型:字符字面量(\b,\f,\\);字符串可以相加;转换为字符串,age.toString();
Alert(String(value));
Object类型:数据和功能的结合;var o = new Object();属性和方法:constructor;hasOwnProperty(propertyName);isPropertypeOf(object);propertyIsEnumberable(properName)(检查属性是否能够使用for-in语句);toLocalString;toSting();valueOf()。
Q2:函数定义和对象定义有哪些?
函数定义:1.函数声明
function fun(){ //expression.... }; |
函数表达式
Var fun = function(){ //expression.... } Var fun1 = function fun2(){ //expression..... }//fun1()=fun2() Var myObject = new object(); myObject.fun = function(){ //expression......... } |
函数表达式
Var fun = new function(){ //expression..... } Var creatfun(parameter){ Return fun1(parameter1,parameter2,...,expression.....); }//使用:creatfun.fun1(parameter1,parameter2.....) |
对象定义:(http://www.w3school.com.cn/js/pro_js_object_defining.asp)
原始方式:var obj = new object; Obj.att= “”;obj.fun = function(){}; |
工厂方式: Function creatObj(){ Var obj1 = new object; Obj1.att = “”; obj1.fun = function(){}; Return obj1; } |
构造函数方式: Function creatObj(){ This.att = “”; this.att1 = “”; obj1.fun = function(){}; } Var Obj = new creatObj(); |
原型方式: Function Obj{ } Obj.att = “”; Obj.fun1= function(){}; Var myobj1 = new Obj(); |
混合的构造函数/原型方式: function Car(sColor,iDoors,iMpg) { this.color = sColor; this.doors = iDoors; this.mpg = iMpg; this.drivers = new Array("Mike","John"); } Car.prototype.showColor = function() { alert(this.color); } var oCar1 = new Car("red",4,23); var oCar2 = new Car("blue",3,25); oCar1.drivers.push("Bill"); alert(oCar1.drivers); //输出 "Mike,John,Bill" alert(oCar2.drivers); //输出 "Mike,John" |
动态原型方法: function Car(sColor,iDoors,iMpg) { this.color = sColor; this.doors = iDoors; this.mpg = iMpg; this.drivers = new Array("Mike","John"); if (typeof Car._initialized == "undefined") { Car.prototype.showColor = function() { alert(this.color); }; Car._initialized = true; } } Var va = new Car(1,2,3); Va.showcolor(); |