0

How to make array in function with options to run. This is declaration:

$(".object").myfunction({
    run1: 'spell',
    run2: true
});

Here is function:

$.fn.myfunction= function() {
    alert(run1);
};

How to alert the run1 or run2 from declaration ?

2
  • Why would you want to do that? why are you adding functionality to the base jQuery object if the functionality doesn't use the object? Commented Jan 16, 2016 at 21:12
  • @Amit That is only example, I am building sth more :) Commented Jan 16, 2016 at 22:52

2 Answers 2

1

Add an object for the function to receive

$.fn.myfunction= function(params) {
    alert(params.run1);
};
Sign up to request clarification or add additional context in comments.

3 Comments

@Mergars, is the function declaration before or after the invocation?
Can you tell me, how spell function without params or with params (2 functions on 1 page) It's desn't works with your result.
javascript cannot overload functions in the way you are suggesting. If you try - only the last will work. What you can do - is check what you have received, i.e. if (params === undefined){/*code for no params*/}else{/*code with params*/}
1

My proposal is:

$.fn.myfunction= function(obj, key) {
  alert(obj[key]);
  /*****
            $.each(obj, function(index, element) {
               alert("Key: " + index + " Value: " + element);
            });
             *****/
};
$(function () {
  $(".object").myfunction({
    run1: 'spell',
    run2: true
  }, 'run1');
});
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>


<button class="object" style="visibility:hidden">Click Me</button>

3 Comments

@Mergars Thanks so much in any case. I updated my snippet.
No problem, but you created function with only 2 params. Thanks again :)
It's a pleasure to cooperate. Thanks so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.