本文介绍了如何使用变量访问对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象
var Messages = {
'fullName' : 'Tell us your cool name dude..! e.g. Yousuf Iqbal',
'userName' : 'Choose a catchy username. Remember! It should be available :)',
'password' : 'Choose a top secret password with special chars, numbers and alphabets',
'rePassword' : 'Retype the password you just typed. But, don\'t try to copy!',
'bYear' : 'Tell the year, in which this bomb blasted'
};
和变量..
var attribute = $('#userinfo form input').attr('name');
现在我想使用这个变量选择Messages对象属性..
now i want to select Messages object property using this variable like this..
var message = Messages.attribute;
但它不工作..
并且还试过以下..
but Its not working..and have also tried the following..
var message = Messages+'.'+attribute;
推荐答案
方括号:
message = Messages[ attribute ];
这篇关于如何使用变量访问对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!