一只叫花花的小小鸟

导航

关于函数的理解

function fn1(){
this.name = "xulihua";
this.mm = function(){
alert(this.name)
}
}
fn1.age = "age";
fn1.init1 = function(){
alert("这是类中的方法");
};
fn1.prototype.age = "PrototypeAge";
fn1.prototype.init = function(){
alert("1");
};
fn1.prototype = {
age : "PrototypeAge",
init : function(){
alert("1");
}
};

fn1.init();//执行报错,因为函数fn1没有init方法(fn1有一个属性age : "age",一个方法init1())
fn1.init1();//正常执行
alert(fn1.name);//undefined,因为没有用到构造函数
alert(fn1.age);//弹出age
var _fn1 = new fn1();//两个属性,name : "xulihua",age : "PrototypeAge",一个方法init()
_fn1.init();//正常执行
_fn1.mm();//正常执行,弹出"xulihua"
_fn1.init1();//执行报错,不存在此方法
alert(_fn1.name);//弹出"xulihua"
alert(_fn1.age);//弹出"PrototypeAge"

posted on 2016-09-19 16:49  一只叫花花的小小鸟  阅读(155)  评论(0编辑  收藏  举报