0

So inside my <script> tags, I have these functions.

$.fn.getRandom = function(min, max) {
    return min + Math.floor(Math.random() * (max - min + 1));
}

function placeRandom() {
    alert(getRandom(1,7));
}

and I call the placeRandom() function like so

$(document).ready(function() {
    placeRandom();
});

when I do this, it gives me an

Object expected

error and refers to the line where I wrote

alert(getRandom(1.7));

Now, my main question is if there is anything wrong with the code which I am showing you, or does it look correct? Because if it looks correct then I think i'm going to have to run through the rest of my entire code and look for maybe a open bracket which isn't closed because that might be the problem. So yea, is there any problem with my placeRandom() or getRandom() function or is the error occuring because of another reason other than these functions?

5
  • 4
    You are binding getRandom to the jQuery namespace, you will only be able to access it through a jQuery object instance or via $.fn.getRandom() Commented Oct 16, 2013 at 14:51
  • with the way you are calling getRandom, i think you just want a standard function. getRandom function(min,max)... Commented Oct 16, 2013 at 14:51
  • Why are you using $.fn? Commented Oct 16, 2013 at 14:52
  • Oh, okay, I will change it to getRandom function() instead. Commented Oct 16, 2013 at 14:54
  • Yes. That's exactly what I mean. Only attach a function to jQuery if you want to integrate it in the library (like a plug-in). See my answer Commented Oct 16, 2013 at 14:55

3 Answers 3

2

The problem is were you are defining the getRandom function. You are putting it in the jQuery.fn namespace. If you organize your code like the following it should work much better.

var getRandom = function(min, max) {
    return min + Math.floor(Math.random() * (max - min + 1));
}

var placeRandom = function() {
    alert(getRandom(1,7));
}

$(document).ready(function() {
    placeRandom();
});

Additional Info: jQuery.fn is actually a prototype but is commonly referred to as a namespace in this specific context.

Sign up to request clarification or add additional context in comments.

2 Comments

That's a prototype, not a namespace.
Very true, I will clarify in my answer.
1

You are binding getRandom to the jQuery namespace, you will only be able to access it through a jQuery object instance or via $.fn.getRandom()

So unless you want to be able to call it like $(selector).getRandom(), just declare it as a standard function.

4 Comments

That's a prototype, not a namespace.
It's a namespace pointing to the jQuery prototype. Wanna argue semantics?
Yes, it will. Try it :)
In JavaScript, functions are first class citizens, which means you can do a lot of fun stuff with them, such as passing them as arguments to other functions, storing their references in variables or using them in objects.
1

you are binding getRandom to the $ namespace (jQuery). so you have to call it $.fn.getRandom(1,7) or define the function the following:

function getRandom(min, max) {
    return min + Math.floor(Math.random() * (max - min + 1));
}

Comments

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.