Web/Javascript

javascript oop first sample

aucd29 2013. 9. 26. 21:35
http://www.javascriptkit.com/javatutors/oopjs2.shtml

[code]
// class
function nabi() {
    this.name = "Hello world";

    this.status = function() {
        alert(this.name);
    }
}

nabi.prototype.changeName = function(name) {
    this.name = name;
};


// instance
deviceapi = new nabi();
deviceapi.status();
deviceapi.changeName('Hello javascript');
deviceapi.status();

// or
var newType = function () {
    this.name = "test";
    this.status = function() {
        alert(this.name);
    }
}

newType.prototype.changeName = function(name) {
    this.name = name;
}

var test = new newType;
test.status();
test.changeName('changedName');
test.status();
[/code]