函数的三种定义
字面量
function声明
function add(){
}
add();
var赋值表达式
var add=function(argument){
};
add();
var add=function fn(argument){
fn();
}
add();
构造函数
new Function(‘num1’,‘num2’,‘return num1+num2;’);
add();
闭包
闭包:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数)
变量作用域:
全局变量
局部变量
//闭包 全局变量在函数内部可以访问
var n=999;
function f1(){
alert(n);//访问到全局n
}
f1();
function f1(){
var n=999;//函数内var 声明的变量,局部变量,省略var是全局变量
}
f1();
alert(n);///n没有定义
//js—》 在f1里面 再定义一个函数
例:
function a(){
var i=0;
function b(){
alert(++i);
}
return b;
}
var c=a();
c();//1
闭包的优缺点
优点:有利于封装,可以访问局部变量
缺点:内存占用浪费严重,内存泄漏
声明对象的方式
js字面式声明对象
var obj ={
属性名称:属性值,
方法名称:function(){},
…
}
例:
var person ={
name:“zhangsan”,
age:26,
sex:“man”,
eat:function(food){
alert(我在吃"+fds);
}
play:function(game){
alert(“我在玩”+game);
}
}
alert(person.name);
person.eat(“面条”);
new操作符后跟Object构造函数
var obj=new Object();
obj.属性=属性值;
obj.属性=属性值;
obj.方法=function(str){
方法代码
};
例:
var box=new Object();
box.name=“zhangsan”;
box.age=100;
box.infos=function(str){
return this.name+"—"+this.age+"—"+str;
}
alert(box.name);
var con=box.infos(“吃饭呐”);
alert(con);
js中构造方法声明对象
function test([参数列表]){
this.属性=属性值;
this.方法=function(){
方法中的代码
}
}
var obj=new test(参数列表);
例:
function’ person(name,sex,age){
this.name=name;
this.sex=sex;
this.age=age;
this.show=function(){
alert(this.name+"—"+this.sex+"—"+this.age);
}
}
var obj1=new person(“zhangsan”,“nan”,18);
alert(obj1.name);
obj1.show();
var obj2=new person(“lisi”,“nv”,20);
obj2.show();
js中工厂方式声明对象
function createObject(name,age){
var obj=new Object();
obj.name=name;
obj.age=age;
obj.run=function(){
return this.name+this.age+‘运行中…’;
};
return obj;
}
var box1=createObject(‘zhangsan’,100);
var box2=createObject(‘lisi’,200);
例:
function createObject(name,age){
var obj=new Object();
obj.name=name;
obj.age=age;
obj.run=function(){
return this.name+"—"+this.age+“运行中…”;
}
obj.say=function(){
return"今天天气不错";
}
return obj;
}
var box1=createObject(“zhangsan”,18);
alert(box1.name);
alert(box1.run());
var box2=createObject(“lisi”,20);
alert(box2.name);
alert(box2.say());
构造和工厂模式不同:
1.构造方式不会显示创建对象,将属性值给this,不需要return对象
2.工厂 在方法内部创建 object对象 ,返回object对象,属性和方法都是赋给object对象
js中原型模式声明对象
function test(){
}
test.prototype.属性=属性值;
test.prototype.属性=属性值;
test.prototype.方法名称=function(){
执行代码
}
var obj=new test();
//让所有实例化的对象都拥有它包含的属性及方法。
例1:
function test(){}
//alert(test.prototype instanceof Object);//自带该对象 prototype是Object子对象
test.prototype.color=“red”;
test.prototype.heighrs=“1.7”;
test.prototype.widths=“1.2”;
test.showInfo=function(){
alert(this.color+"—“this.height+”—"+this.widths);
}
test.getinfo=function(){
alert(“aaa”);
}
var car1 =new test();
alert(car1.color);
car1.showInfo();
car1.getinfo();
例2
function test(){}
//json数据定义属性和方法
test.prototype={
color:“red”,
height:“1.7”,
widths:“1.2”,
showinfo:function(){
alert(this.color+"—“this.height+”—"+this.widths);
}
}
var car1 =new test();
alert(car1.color);
car1.showInfo();
car1.getinfo();
js中混合模式声明对象
function test(v1,v2,v3){
this.v1;
this.v2;
this.v3;
}
test.prototype.方法名称=function(){
执行代码
}
var obj=new Blog(v1,v2,v3);
例:
//混合模式:构造+原型
function blog(name,url,friend){
this.name=name;
this.url=url;
this.friend=frienf;
}
blog.prototype={
test:“awt”,
showinfo:function(){
alert(this.name+"—"+this.url);
},
get:function(){
alert(this.friend);
}
}
var peo =new blog(“zhangsan”,“http://www.baidu.com”,“lisi”);
alert(peo.name);
alert(peo.test);
peo.showinfo();