一、Function对象
在js中,可以把方法当做一个对象,作为参数使用,而在java中方法就是方法,这么做是不允许的。
<script type="text/javascript">
function fun1(name, age) {
alert(name + age);
}
fun1("tom", 21);
//new一个方法,前面是参数,后面是方法体
var fun2 = new Function("name", "age", "alert(name+age)");
fun2("jack", 5);
</script>
Function对象的属性:
var fun2 = new Function("name", "age", "alert(name+age)");
//方法的参数个数,输出为2
alert(fun2.length);
Function对象的方法:
var fun2 = new Function("name", "age", "alert(name+age)");
//输出方法的源码
alert(fun2.toString());
二、JavaScript闭包
闭包:能读取其他函数内部变量的函数
1.js变量的作用域
<script type="text/javascript">
function fun1() {
//var a = 22;
a = 22;
}
fun1();
alert(a);
</script>
在一个方法中定义一个变量var a=22;在方法外是取不到的,如果改成a=22方法外就可以取到。
2.在方法外取到局部变量
<script type="text/javascript">
function fun1() {
var a = 22;
function fun2() {
alert(a);
}
return fun2;//返回一个对象,不是fun2()
}
var result = fun1();//result=fun2;
result();
</script>
<script type="text/javascript">
var name = "Window";
var object = {
name : "object",
getName : function() {
return function() {
//this是Window
return this.name;
};
}
};
alert(object.name);//输出object
//返回一个function对象,调用的话加()
alert(object.getName()());//输出Window
</script>
三、JavaScript面向对象
1.构造对象的两种方式:
<script type="text/javascript">
var obj = {
name : "obj",
action : function() {
alert("会吃");
}
};
alert(obj.name);
obj.action();
function person(name, age) {
this.name = name;
this.age = age;
this.action = function() {
alert("会吃");
};
}
var jack = new person("jack", 1);
alert(jack.name);
jack.action();
</script>
2.对象的属性
<script type="text/javascript">
function obj() {
this.objPro = "对象属性";
obj.prototype.objPro2 = "对象属性2";
var privatePro = "私有属性";
}
obj.classPro = "类属性";
alert(obj.classPro);
var obj = new obj();
alert(obj.objPro);
alert(obj.objPro2);
</script>
3.对象的方法
<script type="text/javascript">
function C() {
var privateFunc = function() {
alert("私有方法");
};
privateFunc();
this.objFunc = function() {
alert("对象方法");
};
C.prototype.objFunc2 = function() {
alert("对象方法2");
};
}
C.classFunc = function() {
alert("类方法");
};
C.classFunc();
var c = new C();
c.objFunc();
c.objFunc2();
</script>
4.js实现继承特性
<script type="text/javascript">
function Animal(name, age) {
this.name = name;
this.age = age;
this.action = function() {
alert("会吃");
};
}
function Dog(name, age) {
Animal.apply(this, [ name, age ]);
}
var jack = new Dog("jack", 1);
//jack是Dog
alert(jack.name);
alert(jack.age);
jack.action();
</script>
原型继承:
<script type="text/javascript">
function Animal(name, age) {
this.name = name;
this.age = age;
this.action = function() {
alert("会吃");
};
}
function Dog(name, age) {
Animal.apply(this, [ name, age ]);
}
//原型继承
Dog.prototype = new Animal();
var jack = new Dog("jack", 1);
//jack是Animal
alert(jack.name);
alert(jack.age);
jack.action();
</script>
5.js实现多态特性
<script type="text/javascript">
function Animal() {
this.say = function() {
alert("我是动物");
};
}
function Dog() {
this.say = function() {
alert("我是狗");
};
}
Dog.prototype = new Animal();
function Cat() {
this.say = function() {
alert("我是猫");
};
}
Cat.prototype = new Animal();
function say(animal) {
if (animal instanceof Animal) {
animal.say();
}
}
var dog = new Dog();
var cat = new Cat();
say(dog);//输出我是狗
say(cat);//输出我是猫
</script>