javascript - dot-notation vs. bracket notation -
this question has answer here:
i have javascript function:
function test() { var xhr = new xmlhttprequest(); xhr.onreadystatechange = function() { if (xhr.readystate==4 && xhr.status==200) { var obj = json.parse(xhr.responsetext); document.title = obj.page_title; } } xhr.open("get", "title.php", true); xhr.send(); }
my question difference between: obj.page_title
, obj["page_title"]
work fine me , return same value.
the 2 same:
https://developer.mozilla.org/en-us/docs/web/javascript/guide/working_with_objects
the brackets option useful if want use variable:
var key = 'page_title'; obj[key];
or if attribute names have unusual characters spaces in them:
var obj = { 'title of page': 'title' } obj.'title of page' // wrong obj['title of page'] // right
Comments
Post a Comment