JavaScript访问对象属性跟方法
JavaScript访问对象属性和方法
访问对象的属性
在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);