摘要:本篇教程介绍了JavaScript基础教程 JavaScript中给对象添加函数的方式,希望阅读本篇文章以后大家有所收获,帮助大家对JavaScript的理解更加深入。
<
function 类名(){
this.属性;
}
var 对象名=new 类名();
function 函数名(){
//执行代码
}
对象名.属性名=函数名;
对象名.属性名();
function Person(){
this.name="abc";
this.age=30;
}
function show1(){
window.alert("hello"+this.name);
}
var p1=new Person();
p1.abc=show1;
p1.abc();
2. 对象名.属性名=function(参数列表){
//代码
}
//调用
对象名.属性名(实际参数);
function Person(){
this.name="xx";
this.age=20;
}
var p1=new Person();
p1.abc=function show1(){
window.alert("hello"+this.name);
}
p1.abc();
3. 原型法(多个对象可共享函数)
function Dog(){
}
var dog1=new Dog();
Dog.prototype.shout=function(){
window.alert("dog");
}
dog1.shout();
var dog2=new Dog();
dog2.shout();
4. 使用传入的实参初始化属性
function Person(name,age){
this.name=name;
this.age=age;
}
this.show=function(){
window.alert("hello"+this.name);
}
var p1=new Person("aa",21);
p1.show()
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标WEB前端JavaScript频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号