简体   繁体   中英

How to call another class object using literal class in javascript

I am trying to create playlist class object from Main class function.

function playlist(){
    playlist.prototype.check = function (){
        alert(2);
    };
    this.check();
};

Main = {};//literal class
Main.test = function(){
    var obj  = new playlist();
}

Why am I not able to get the alert?

Ok my friend

There's a simple solution for this To do it, we need to create the class declaration, then we make a prototype playlist class.

In this example i prefer to use first-capitalize letter on naming class in JS.

// Playlist class declaration
function Playlist() {
    // Class properties
    // for example: this.songs = [];
    // for example: this.startingIndex = 0;
}

// Adds extra behavior for Playlist class
Playlist.prototype.check = function () {
    alert(2);
};

// Creates an instance from Playlist
var playlistObj = new Playlist();

// Calls check method.
playlistObj.check();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM