【问题标题】:refer to options within variables in jQuery在 jQuery 中引用变量中的选项
【发布时间】:2011-12-28 02:24:15
【问题描述】:

只是对 jQuery/Javascript 和引用变量中的选项感到好奇。

所以,我写了一个小插件,当我调用“填充”时会填充一些文本。很基础。这是我正在尝试做的事情:

var foo = {
    msg1: "Hey",
    msg2: this.msg1 + "how",
    msg3: this.msg2 + "are",
    msg4: this.msg3 + "you",
    msg5: this.msg4 + "doing",
    msg6: this.msg5 + "today"
}

$("#bar").populate({text: foo.msg6});

因此,当调用 .populate 插件时,#bar 会添加“嘿,你今天过得怎么样”。

populate 插件很难解释,但相信我,它工作得很好,而不是问题。我只是好奇这种变量和选项的组织方式是否可行,因为对我来说,它看起来更干净、更“语义化”、更有条理,而不仅仅是一堆引用其他变量的变量。 ..

希望我说得有道理!谢谢大家!

【问题讨论】:

标签: javascript jquery variables plugins options


【解决方案1】:

this 仅在 execution context 中有意义。定义函数时,会为该函数创建一个作用域链,只要该函数进入当前执行上下文,就会使用该作用域链。发生这种情况时,this 将被定义(在运行时)。详情请见this。

在您的示例中,您没有定义任何新的范围链,因为您没有在对象上创建任何方法。因此,当您引用 this 时,您在创建 foo 对象时指的是 this 的任何内容。最有可能的是,您正在全局空间中工作,因此 this 将是 window(如果您在浏览器中),但它可能是另一个具有您当前正在执行的函数的对象。例如:

    var myScopeObj = {
        myMethod: function() {
            var foo = {
                msg1: "Hey",
                msg2: this.msg1 + "how",
                msg3: this.msg2 + "are",
                msg4: this.msg3 + "you",
                msg5: this.msg4 + "doing",
                msg6: this.msg5 + "today"
            };
            var foo2 = foo.msg6;
        }
    };

上面的例子有两种可能性:

    myScopeObj.myMethod();  // 'this' refers to myScopeObj

或

    var method = myScopeObj.myMethod;
    method();  // 'this' refers to window

或

    myScopeObj.myMethod.call(window);  // 'this' refers to window

您可以做的是为您的对象创建一个构造函数,并使用this 来映射属性。

    function Foo() {
        this.msg1 = "Hey";
        this.msg2 = this.msg1 + "how";
        this.msg3 = this.msg2 + "are";
        this.msg4 = this.msg3 + "you";
        this.msg5 = this.msg4 + "doing";
        this.msg6 = this.msg5 + "today";
    };
    var foo = new Foo();
    $("#bar").populate({text: foo.msg6});

【讨论】:

  • 是的!我一开始就明白你说的所有东西。但是底部的建议非常有效。非常感谢你!惊人的。你摇滚。我之前尝试过这样做,但忘记在实际变量声明本身中有this.。很棒的小费,我真的很感激。
【解决方案2】:

在像你这样的对象定义中,this 并不引用对象本身,所以你的对象声明不会做你想做的事。

事实上,在this sample code 中,您可以看到该对象定义中的this 是window 对象,这是javascript 解释器在没有其他应该设置的值时将其设置为的对象有。

我不明白你为什么要尝试做你正在做的事情(所以我不能提出更好的替代方案),但你可以使用 init() 方法最终得到所需的 foo 对象:

var foo = {
    msg1: "Hey",
    init: function() {
        this.msg2 = this.msg1 + "how";
        this.msg3 = this.msg2 + "are";
        this.msg4 = this.msg3 + "you";
        this.msg5 = this.msg4 + "doing";
        this.msg6 = this.msg5 + "today";
    }
}

foo.init();
$("#bar").populate({text: foo.msg6});

或者您可以将其与构造函数一起使用:

function Foo() {
    this.msg1 = "Hey";
    this.msg2 = this.msg1 + "how";
    this.msg3 = this.msg2 + "are";
    this.msg4 = this.msg3 + "you";
    this.msg5 = this.msg4 + "doing";
    this.msg6 = this.msg5 + "today";
}

var foo = new Foo();
$("#bar").populate({text: foo.msg6});

【讨论】:

  • 是的,我知道。这就是我要找的。有没有办法正确映射这些?抱歉,如果问题不是很清楚,很难解释!感谢您的输入。
  • 我添加了几个选项来生成所需的 foo 对象,尽管我认为可能有更好的方法来完成你的结果(如果我知道你真正想要做什么)。
猜你喜欢
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 2010-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多