简体   繁体   English

如何在两个javascript对象文字之间进行通信?

[英]How do I communicate between two javascript object literals?

I want to know how I can call a method from an object within another object literal. 我想知道如何从另一个对象文字中的对象调用方法。 Here is my code. 这是我的代码。 I am building a jQuery UI slider. 我正在构建一个jQuery UI滑块。 I am trying to call the animatebox function within the animations object literal and the error in my chrome console is: 我正在尝试在animations对象文字中调用animatebox函数,而我的chrome控制台中的错误是:

Uncaught TypeError: Cannot call method 'animateBox' of undefined

My code looks like this: 我的代码如下所示:

var sliderOpts = {

    animate: true,
    range: "min",
    value: 50,
    min: 10,
    max: 100,
    step: 10,

    //this gets a live reading of the value and prints it on the page
    slide: function (event, ui) {
        $("#slider-result").html(ui.value + " GB");
    },

    //this updates the hidden form field so we can submit the data using a form
    change: function (event, ui) {
        $('#hidden').attr('value', ui.value);
        var val = ui.value,
            $box = $('#message');
        switch (true) {
        case (val > 50):
            $box.animations.animateBox().html('you have chosen xtremenet');
            break;
        case (val < 50):
            $box.fadeOut().fadeIn().html('you have chosen valuenet');
            break;
        default:
            $box.fadeOut().fadeIn().html('you have chosen powernet');
        }
    }
};

var animations = {
    animateBox: function () {
        $("#message").slideDown(500);
    }
};

$(".slider").bind("slidecreate", animations.animateBox).slider(sliderOpts);

So how do I call this method called animateBox? 因此,如何调用称为animateBox的方法?

$box.animations - is undefined - and it is so $box.animations未定义-确实如此

the element with id="message" wrapped with a jQuery object, doesn't have the animations property 带有jQuery对象的id =“ message”元素没有动画属性

in your case you can fix it by simply calling 您可以通过简单地致电来解决

animations.animateBox();

instead of $box.animations.animateBox() 而不是$box.animations.animateBox()

The jquery object $box has no .animations property. jQuery对象$ box没有.animations属性。 You have defined animations as a global (window-level) variable. 您已将动画定义为全局(窗口级)变量。

Also chaining won't work because you don't return anything from the animateBox function. 同样,链接将不起作用,因为您不会从animateBox函数返回任何内容。 I guess you want to change that function to return $('#message'). 我猜您想更改该函数以返回$('#message')。 Once you have that, instead of 一旦有了它,而不是

$box.animations.animateBox().html('you have chosen xtremenet');

try 尝试

animations.animateBox().html('you have chosen xtremenet');

You should just be able to call animations.animateBox() . 您应该只能够调用animations.animateBox() You haven't set it up as a jQuery method, so $box.animations is likely what is throwing the error. 您尚未将其设置为jQuery方法,因此$box.animations很可能$box.animations错误。

var animations = {
    animateBox: function (obj) {
        obj.slideDown(500);
    }
};


animations.animateBox($box.html('you have chosen xtremenet'))

You could supply the animateBox function as a callback via the options. 您可以通过选项提供animateBox函数作为回调。 Like so: 像这样:

var sliderOpts = {
    animateBox: function() { // magic here },
    animate: true,
    range: "min",
    ...
};

then set the slider's animateBox value within your plugin. 然后在插件中设置滑块的animateBox值。 If you use standard plugin conventions the call to animate box in change would look like: 如果使用标准插件约定,则更改中的动画框调用将如下所示:

this.animateBox();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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