我们可以在对象中运行某些javascript吗?

时间:2022-09-11 10:35:48

I am new at JavaScript objects and wondering if we could run JavaScript in an Object? This is what I want to do-

我是JavaScript对象的新手,想知道我们是否可以在Object中运行JavaScript?这就是我想做的事情 -

$('.element').click(function () {
    var data = $(this).attr('data-home');
    if (data !== undefined) {
        var jsRun = {
            a: $('.more').show(),
            b: $('.div').show(),
            c: window.location = "www.url.com",
            d: location.reload()
        };
    }
    return jsRun[data];
});

I wrote return jsRun because I'm not quite sure what to do if this is allowed and how to make it run the code that I specified. Any suggestions would help thanks

我写了返回jsRun因为我不太确定如果允许该怎么做以及如何让它运行我指定的代码。任何建议都会有所帮助

4 个解决方案

#1


5  

Enclose all of those in functions, after which they can be invoked as methods of the object.

将所有这些包含在函数中,之后可以将它们作为对象的方法调用。

$('.element').click(function () {
    var data = $(this).attr('data-home'),
        jsRun; //your variable declaration is hoisted to here anyway
    if (data !== undefined) {
        jsRun = {
            a: function(){
               $('.more').show();
            },
            ...
        };
        return jsRun[data]();
    }
});

The result, assuming data is "a", is that $('.more') is shown. I've moved the invocation inside the conditional block, since I'm assuming you only want this to happen if data has a value.

假设数据为“a”,结果是显示$('。more')。我已经在条件块中移动了调用,因为我假设您只希望在数据有值时发生这种情况。

#2


4  

Use a switch :

使用开关:

$('.element').on('click', function() {
    switch ( $(this).data('home') ) {
       case 'a': $('.more').show();
         break;
       case 'b': $('.div').show();
         break;
       case 'c':  window.location = "www.url.com";
         break;
       case 'd':  location.reload();
         break;
       default:
                  goNuts(); // do something if none of the above 
    }
});

#3


2  

You need to use functions;

你需要使用功能;

a: function(){$('more').show();}

Then call

然后打电话

jsRun[data]();

(Note the brackets)

(注意括号)

#4


0  

You are not running this code inside the object.

您没有在对象内运行此代码。

You are running it before the object creation. Anything that these expressions will return will be set into the keys where you placed the expression.

您在创建对象之前运行它。这些表达式将返回的任何内容都将设置到放置表达式的键中。

Be aware that any expression returns something, at least: undefined

请注意,任何表达式都会返回一些内容,至少:undefined

#1


5  

Enclose all of those in functions, after which they can be invoked as methods of the object.

将所有这些包含在函数中,之后可以将它们作为对象的方法调用。

$('.element').click(function () {
    var data = $(this).attr('data-home'),
        jsRun; //your variable declaration is hoisted to here anyway
    if (data !== undefined) {
        jsRun = {
            a: function(){
               $('.more').show();
            },
            ...
        };
        return jsRun[data]();
    }
});

The result, assuming data is "a", is that $('.more') is shown. I've moved the invocation inside the conditional block, since I'm assuming you only want this to happen if data has a value.

假设数据为“a”,结果是显示$('。more')。我已经在条件块中移动了调用,因为我假设您只希望在数据有值时发生这种情况。

#2


4  

Use a switch :

使用开关:

$('.element').on('click', function() {
    switch ( $(this).data('home') ) {
       case 'a': $('.more').show();
         break;
       case 'b': $('.div').show();
         break;
       case 'c':  window.location = "www.url.com";
         break;
       case 'd':  location.reload();
         break;
       default:
                  goNuts(); // do something if none of the above 
    }
});

#3


2  

You need to use functions;

你需要使用功能;

a: function(){$('more').show();}

Then call

然后打电话

jsRun[data]();

(Note the brackets)

(注意括号)

#4


0  

You are not running this code inside the object.

您没有在对象内运行此代码。

You are running it before the object creation. Anything that these expressions will return will be set into the keys where you placed the expression.

您在创建对象之前运行它。这些表达式将返回的任何内容都将设置到放置表达式的键中。

Be aware that any expression returns something, at least: undefined

请注意,任何表达式都会返回一些内容,至少:undefined