The content of this article is to introduce how js accesses the properties and methods of objects? (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The properties and methods of an object are collectively called members of the object.
Accessing the properties of an object
In JavaScript, you can use "." and "[] ” to access the properties of the object.
The difference between the two: "." notation is generally used as a static object to access properties. The "[ ]" notation is very useful when accessing attributes dynamically.
var object = {name:'xiaoming',age:29}; var name1 = object.name;var name2 = object['name'];
Methods to access objects
In JavaScript, you can only use "." to access methods of objects .
function Person(){ this.name = 'xiaoming'; this.age = 29; this.say = function(){ alert('This is person'); } } var student = new Person(); alert(student.name); alert(student['age']); student.say(); //使用" . "访问对象方法
Two questions derived from:
1. Determine the number of occurrences in a string The most characters, count the number
var str = 'Thisthebesttimesewerty'; var obj = {}; for(var i=0; i Copy after login
2. Write the arraysSimilar function to determine whether the two incoming arrays are similar.
Specific requirements:
1. The members in the array have the same type, but the order can be different. For example, [1, true] is similar to [false, 2].
2. The length of the arrays is the same.
3. The type judgment range needs to be distinguished: String, Boolean, Number, undefined, null, function, date, window.
When all the above are satisfied, "Judgment Result: Pass" is returned, otherwise "Judgment Result: Fail" is returned.
function arraysSimilar(arr1,arr2){ if(!(arr1 instanceof Array) || !(arr2 instanceof Array)){ return false; } if(arr1.length !== arr2.length){ return false; } var i=0, n=arr1.length, countMap1={}, countMap2 = {}, t1, t2, TYPES = ['string','boolean','number','undefined','null','function','date','window']; for(i; i Copy after login
The above is the detailed content of How to access the properties and methods of objects in js? (code example). For more information, please follow other related articles on the PHP Chinese website!