logo javascript

Javascript Course for Beginners

Instructor: Ivan Lourenço Gomes

Objects methods

Objects values can be functions. In this case we call them methods. Methods can use other object properties using the keyword this.

                    

    var student = {
        'firstName': 'Marie',
        'lastName': 'Smith',
        'fullName': function() {
            return this.firstName + ' ' + this.lastName;
        }
    }

    console.log( student.fullName() ); // The console will show 'Marie Smith'


                    
                

Important:

Did you know that everything in Javascript is an object?

Javascript is an object oriented programming language so it treats basically everything as an object.

Objects, arrays, functions and everything except for the primitive data types, are objects.

Although the strings and numbers primitives are not considered to be objects, they have methods and other object-like features.

Let's recap a few times we worked with methods:

                    
    console.log('Hello World'); // log is a method of the console object 

    Math.round(2.7);  // round is a method of the Math object

    var num = 3; 
    var num_string = num.toString(); // toString is a method that can be applied to different data types.

    document.getElementById("caixa_azul").innerHTML;  // getElementById is a method of the document object.
    // innerHTML is a property of the object returned by the get method.


    var courses = [ "HTML", "Python", "PHP" ];
    courses.push("Javascript"); // push is a method of the array data type.


                    
                

We won't dive deeper into this subject now but you can learn more here.

Contents