码迷,mamicode.com
首页 > 其他好文 > 详细

关于函数的理解

时间:2016-09-19 17:56:50      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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"

关于函数的理解

标签:

原文地址:http://www.cnblogs.com/huahua-1022/p/5885767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!