获取变量的名称

时间:2014-05-12 13:03:03

标签: javascript html

在此代码中:

function myType(string){
    this.string = string;
    this.getName = function(){
        //return this.name;
    }
}
var myVar = new myType("string");

如果我使用myVar.getName(),则应返回myVar。我需要它来创建一个弹出框来改变页面的整个<body>,所以对我来说这很重要。

我该怎么办?


编辑:

我要做的是编写一个自定义对象。这是步骤:

  1. 用户使用字符串创建对象;
  2. 我定义了这些功能:
    (1)call():它调用内联弹出框,它将页面的整个部分更改为弹出框;它将当前正文保存在this.document_content中 (2)hide():使用this.document_content将正文更改为document.body.innerHTML
  3. 使用`[变量名称] .call();“
  4. 进行用户调用
  5. 用户可以通过单击其中的<a>按钮关闭弹出框。
  6. 我发现问题出在2(1),我无法将变量名称放在onclick的代码中。

    我该怎么办?

1 个答案:

答案 0 :(得分:0)

如果你想改变你的构造函数,你可以这样做 -

function myType(name, string){
    this.string = string;
    this.name = name;
    this.getName = function(){
        return this.name;
    }
}
var myVar = new myType("myVar","string");

或者你可以创建一个哈希:

function myType(name, string){
        this.string = string;
        this.name = name;
        this.getName = function(){
            return this.name;
        }
        var res = {}
        res[name] = this;
        return res;
    }
// Add response to an hash
var hash = {}
$.extend(hash, new myType("myVar","string"));