js函数
一、js最常见的函数定义方式:
-
1、函数声明,使用function语句
-
function print(x,y){
-
return x*y;
-
}
-
2、函数直接量定义函数
-
var print = function(x,y){
-
return x*y;
-
}
-
3、构造函数方法 new Function();
-
var print = new Function('a','b','return a+b;'二、js对象创建
-
1、对象直接量/字面量
-
var obj = {
-
name:'li',
-
age:18,
-
sex:'nv'
-
}
-
console.log(obj.name); //li
2、构造函数
(1)、系统自带的,eg: new Object(), Array(), Number(),Boolean(), Date()...
var obj = new Object();
obj.name = 'yue';
obj.age = 18;
obj.sex = 'nv';
console.log(obj.name);(2)、自定义的,为了和普通函数区分,首写字母大些,采用驼峰式写法(普通函数采用小驼峰式写法)
function Obj(name){
this.name = name;
this.age = 18;
}
function Obj2(sex){
this.sex = sex;
}
var obj = new Obj('wu');
var obj2 = new Obj2('nv');
console.log(obj.name);
console.log(obj.age);
console.log(obj2.sex);
-