Home > Web Front-end > JS Tutorial > body text

How to access the properties and methods of objects in js? (code example)

青灯夜游
Release: 2018-10-24 17:57:41
forward
2338 people have browsed it

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'];
Copy after login

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();  //使用" . "访问对象方法
Copy after login

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<str.length;i++){
    var char = str[i];
 // var char = str.charAt(i); //charAt()可返回指定位置的字符,i为字符的下标,从0开始
    if(obj[char]){
        obj[char]++; //char是obj对象的一个属性,如果存在次数加1
    }else{
        obj[char] = 1; //如果不存在,保存在obj中并计数为1
    }
}

var max = 0;
var maxChar = null;
for(var key in obj){
    if(max < obj[key]){
        max = obj[key]; //出现最多次数max
        maxChar = key;  //出现次数最多的字符maxChar
    }
}
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<n; i++){
         t1 = typeOf(arr1[i]);
         t2 = typeOf(arr2[i]);
         if(countMap1[t1]){
             countMap1[t1]++;
         }  else{
             countMap1[t1] = 1;
         }

        if(countMap2[t2]){
             countMap2[t2]++;
         }  else{
             countMap2[t2] = 1;
         }
    }
 
    for(i=0; i<TYPES.length; i++){
        if(countMap1[TYPES[i]] != countMap2[TYPES[i]]){
            return false;
        }
    }
    return true;
}

function typeOf(ele){
    var r;
    if(ele === null){
        r = 'null';
    }
    else if(ele === 'window'){
        r = 'window';
    }
    else if(ele instanceof Array){
        r  = 'array';
    }
    else if(ele instanceof Date){
        r = 'date';
    }
    else{
        r = typeof(ele);
    }
    return r;
}
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!

Related labels:
js
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!