Javascript 如何找到创建对象的变量的名称?
我有一个对象,它被两个不同的变量调用。有没有办法在对象函数内部找出调用它的变量Javascript 如何找到创建对象的变量的名称?,javascript,html,Javascript,Html,我有一个对象,它被两个不同的变量调用。有没有办法在对象函数内部找出调用它的变量 sp = new SelectedPlaylists("tableSelectedPlaylists"); sa = new SelectedPlaylists("tableSelectedAlbums"); 这是两个变量我认为您可以在SelectedPlaylists类中创建一个变量,以确定哪种类型的播放列表并通过构造函数传递: sp = new SelectedPlaylists("tableSelec
sp = new SelectedPlaylists("tableSelectedPlaylists");
sa = new SelectedPlaylists("tableSelectedAlbums");
这是两个变量我认为您可以在
SelectedPlaylists
类中创建一个变量,以确定哪种类型的播放列表并通过构造函数传递:
sp = new SelectedPlaylists("tableSelectedPlaylists", "p");
sa = new SelectedPlaylists("tableSelectedAlbums", "a");
在承包商中:
function SelectedPlaylists(id, type)
{
// ... Your code ...
var manageType=type;
var buttonLabel='';
switch(type)
{
case 'p':
// Playlist mode
this.buttonLabel='Remove Playlist';
case 'a':
// Album mode
this.buttonLabel='Remove Album';
}
}
将第二个参数存储在变量中(例如manageType
),并在需要时使用(查看textOnClick
var):
SelectedPlaylists.prototype.addPlaylist=函数(id、名称)
{
如果(!id)返回-1;
var=这个;
var tablebody=this.reftable.children('tbody');
var=false;
var rownode=this.getRowNodebyPlaylistID(id);
如果(行节点)
{
返回-1;
}
this.arrPlaylists.push({id:id,name:name});
demo=此类型的类型;
tablebody.append(“+name+”“+id+”“+this.buttonLabel+”);
因此,代码更易于重用和控制,所有配置都在构造函数中完成
希望有帮助!不,您不能。请注意,可能甚至没有用于存储对象的变量……类似于
L.push(new Foo(1))
的代码是完全有效的,并且不会使用命名变量来存储创建的对象)
在这些情况下,通常要做的是向对象构造函数提供其他参数,以便您可以在处理过程中使用这个额外的参数来决定要做什么
另外,如果在闭包中将实际处理移出构造函数,这样会增加相同代码在其他情况下再次被重用的可能性,这也会更好…例如:
function addButton(text, classname, container, f) {
var b = document.createElement("input");
b.type = "button";
b.value = text;
b.className = classname;
container.appendChild(b);
b.onclick = f;
return b;
}
addButton("b1", "redbutton", menu, function() { alert("You pressed b1"); });
addButton("b2", "bluebutton", menu2, function() { alert("You pressed b2"); });
通过闭包
f
您将在按下按钮时可以使用该按钮做什么方面留下绝对的自由我相信您的问题措辞有点不准确。我认为您的意图是,当单击一个按钮以删除时,然后在首先创建行的对象上执行RemoveOWWithTD。
为此,一种方法是在将元素添加到DOM中后附加事件处理程序,然后传递一个引用创建元素的实例的函数。我做了一些调整,因为您没有提供完整的代码,但下面的内容在浏览器中对我有用,可以准确地将行添加到相应的表中并删除请注意,代码中的注释很重要。我必须进行更改才能使其在我这方面起作用
<!DOCTYPE HTML>
<html>
<head>
<title>Testing Script</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
.playlist_button {
background: none !important;
border: none;
padding: 0 !important;
border-bottom: 1px solid #444;
cursor: pointer;
}
</style>
</head>
<body>
<table id="tableSelectedPlaylists">
<tbody></tbody>
</table>
<table id="tableSelectedAlbums">
<tbody></tbody>
</table>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
// A placeholder definition
SelectedPlaylists = function(tableId) {
//Assumed functionality
this.reftable = $('#' + tableId);
this.arrPlaylists = [];
this.removeRowWithTd = function(source) {
source.parent().parent().remove();
}
};
//Extension to the prototype
SelectedPlaylists.prototype.addPlaylist = function(id, name) {
if (!id)
return -1;
var that = this;
var tablebody = this.reftable.children('tbody');
var found = false;
/* Removed ONLY for my testing as it served no purpose
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
// Next line adds id for reference after being added to DOM
+ '</td><td><button id="' + this.reftable.id + '_button_' + id
// Yes, I moved your CSS into the stylesheet and replaced with a class
+ '" class="playlist_button">Remove</button></td></tr>');
//Now, attach an onClick handler as the return of a function which gets passed itself
//and then returns another function which executes the desired method.
$('#' + this.reftable.id + '_button_' + id).on('click', function(instanceObj) {
return function() {
instanceObj.removeRowWithTd($(this));
}
}(this));
}
//Instantiation
sp = new SelectedPlaylists("tableSelectedPlaylists");
sa = new SelectedPlaylists("tableSelectedAlbums");
//Calling the new method
sa.addPlaylist(1, 'ABC');
sp.addPlaylist(2, 'DEF');
</script>
</body>
</html>
测试脚本
.播放列表按钮{
背景:无!重要;
边界:无;
填充:0!重要;
边框底部:1px实心#444;
光标:指针;
}
//占位符定义
SelectedPlaylists=功能(tableId){
//假定功能
this.reftable=$('#'+tableId);
this.arrPlaylists=[];
this.removowWithTD=函数(源){
source.parent().parent().remove();
}
};
//原型的扩展
SelectedPlaylists.prototype.addPlaylist=功能(id、名称){
如果(!id)
返回-1;
var=这个;
var tablebody=this.reftable.children('tbody');
var=false;
/*仅为我的测试而移除,因为它没有任何用途
var rownode=this.getRowNodebyPlaylistID(id)
如果(行节点){
返回-1;
}
*/
此.arrlists.push({
id:id,
姓名:姓名
});
demo=此类型的类型;
tablebody.append(“”+名称
+''+id
//下一行在添加到DOM后添加id以供参考
+“删除”);
//现在,附加一个onClick处理程序作为自身传递的函数的返回
//然后返回另一个执行所需方法的函数。
$('#'+this.reftable.id+'_按钮'+id).on('click',函数(instanceObj){
返回函数(){
instanceObj.removowwithtd($(this));
}
}(本);;
}
//实例化
sp=新选择的播放列表(“表选择的播放列表”);
sa=新选择的播放列表(“表选择的播放列表”);
//调用新方法
sa.添加播放列表(1,“ABC”);
sp.addPlaylist(2,'DEF');
最后,断言if
是一种糟糕的编程实践是荒谬的。在存在其他替代方案的地方使用它们可以使脚本更高效(在循环不变性等情况下)。虽然我对汇编几乎一无所知。请明智地使用您的工具,但不要因为缺乏技能而清空工具箱。现在,如果您想向制定此规则的人提出挑战,请告诉他们编写脚本时不带条件,只使用
愉快的编码!嘿。感谢您的及时回复。不幸的是,我们不允许使用ifs,因为这是一种糟糕的编程实践。代码可能会在很多地方恢复。如果允许使用ifs,我也可以使用这个.tableid。没有其他方法吗?对不起,我不同意。我想知道如果您有调用变量,您会怎么做:if。我想你可以在构造函数中配置所有参数,看看变化。这是我听说过的最奇怪的“最佳实践”?(我想知道,也有点担心,没有人问为什么TO想知道变量名。)收件人:为什么您想知道调用
SelectedPlaylists
实例的变量名是什么?您想如何处理此信息?
<!DOCTYPE HTML>
<html>
<head>
<title>Testing Script</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
.playlist_button {
background: none !important;
border: none;
padding: 0 !important;
border-bottom: 1px solid #444;
cursor: pointer;
}
</style>
</head>
<body>
<table id="tableSelectedPlaylists">
<tbody></tbody>
</table>
<table id="tableSelectedAlbums">
<tbody></tbody>
</table>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
// A placeholder definition
SelectedPlaylists = function(tableId) {
//Assumed functionality
this.reftable = $('#' + tableId);
this.arrPlaylists = [];
this.removeRowWithTd = function(source) {
source.parent().parent().remove();
}
};
//Extension to the prototype
SelectedPlaylists.prototype.addPlaylist = function(id, name) {
if (!id)
return -1;
var that = this;
var tablebody = this.reftable.children('tbody');
var found = false;
/* Removed ONLY for my testing as it served no purpose
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
// Next line adds id for reference after being added to DOM
+ '</td><td><button id="' + this.reftable.id + '_button_' + id
// Yes, I moved your CSS into the stylesheet and replaced with a class
+ '" class="playlist_button">Remove</button></td></tr>');
//Now, attach an onClick handler as the return of a function which gets passed itself
//and then returns another function which executes the desired method.
$('#' + this.reftable.id + '_button_' + id).on('click', function(instanceObj) {
return function() {
instanceObj.removeRowWithTd($(this));
}
}(this));
}
//Instantiation
sp = new SelectedPlaylists("tableSelectedPlaylists");
sa = new SelectedPlaylists("tableSelectedAlbums");
//Calling the new method
sa.addPlaylist(1, 'ABC');
sp.addPlaylist(2, 'DEF');
</script>
</body>
</html>