User Tools

Site Tools


javascript_object_methods

Back to Javascript and React

Javascript Object Methods

var person = {
    firstName: "John",
    lastName : "Doe",
    id       : 5566,
    fullName : function() {
        return this.firstName + " " + this.lastName;
    }
};

alert(person.fullName());

>> John Doe

If you omit the (), it will return the function definition

var person = {
    firstName: "John",
    lastName : "Doe",
    id       : 5566,
    fullName : function() {
        return this.firstName + " " + this.lastName;
    }
};

alert(person.fullName);

>> function() { return this.firstName + " " + this.lastName; }

Adding a function to an existing object

person.name = function () {
    return this.firstName + " " + this.lastName;
};
javascript_object_methods.txt · Last modified: 2018/08/30 19:49 by admin