访问对象的属性
在JavaScript中,可以使用" . " 和 " [ ] " 来访问对象的属性。
1.使用" . "来访问对象属性
objectName.propertyName
其中,objectName为对象名称,propertyName为属性名称。
2.使用" [ ] "来访问对象属性
objectName[propertyName]
访问对象的方法
在JavaScript中,只能使用" . "来访问对象的方法。
objectName.methodName()
其中,methodName()为函数名称。
创建一个Person类
function Person(){
this.name = "丁亮";
this.gender = "男";
this.age = "20";
this.say = function(){
return "我的名字是" + this.name + "性别是" + this.gender + "今年" + this.age
}
}
var figure = new Penson();
alert("姓名:" + figure.name);
alert("性别:" + figure["gender"]);
alert(figure.say);
本文介绍了在JavaScript中如何通过.和[]符号访问对象的属性与方法,包括创建Person类实例并调用其方法。
876

被折叠的 条评论
为什么被折叠?



