javascript - How to find the name of variable where object is created? -
i have object called 2 different variables. there way find out - inside object function - variable calling it?
sp = new selectedplaylists("tableselectedplaylists"); sa = new selectedplaylists("tableselectedalbums");
these 2 variables
i think create variable inside selectedplaylists
class determine type of playlists , pass trhough constructor:
sp = new selectedplaylists("tableselectedplaylists", "p"); sa = new selectedplaylists("tableselectedalbums", "a");
in consctructor:
function selectedplaylists(id, type) { // ... code ... var managetype=type; var buttonlabel=''; switch(type) { case 'p': // playlist mode this.buttonlabel='remove playlist'; case 'a': // album mode this.buttonlabel='remove album'; } }
store second param in variable (for example managetype
) , use when needed (take textonclick
var):
selectedplaylists.prototype.addplaylist = function (id, name) { if( !id ) return -1; var = this; var tablebody = this.reftable.children('tbody'); var found = false; var rownode = this.getrownodebyplaylistid(id); if(rownode) { return -1; } this.arrplaylists.push({id:id,name:name}); demo = typeof this; tablebody.append("<tr><td class='name'>"+name+"</td><td class='id'>"+id+"</td><td><button style='background:none!important;border:none;padding:0!important;border-bottom:1px solid #444; cursor: pointer' onclick='sp.removerowwithtd( $(this)); console.log($(this))'>"+this.buttonlabel+"</button></td></tr>");
thus, code better reuse , control , configuration done in constructor.
hope helps!
Comments
Post a Comment