Call object member function : Member Function « Object Oriented « JavaScript Tutorial






<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function createCar(color, doors, mpg) {
    var tempcar = new Object;
    tempcar.color = color;
    tempcar.doors = doors;
    tempcar.mpg = mpg;
    tempcar.showColor = function () {
        alert(this.color)
    };

    return tempcar;
}

var car1 = createCar("red", 4, 23);
var car2 = createCar("blue", 3, 25);
car1.showColor();
car2.showColor();


</script>

</body>
</html>








25.4.Member Function
25.4.1.Defining the object methods outside of the factory functions and then pointing to them
25.4.2.Create an Object with member method
25.4.3.Add member function
25.4.4.Adding method to a class using prototype
25.4.5.Custom objects and associative object arrays
25.4.6.Instance method
25.4.7.Call object member function