Javascript arguments as array? - javascript

so I have a button with this event:
onmousedown="hideElements('\x22cartview\x22,\x22other\x22')"
and then this function hideElements:
function hideElements(what)
{
var whichElements=[what];
alert(whichElements[0]);
}
I want it to alert "cartview" but it alerts
"cartview","other"
I am aware of the arguments object but in this case I don't know how to use it to access the individual strings that are comma separated. Probably there is an easy solution but I am kind of new to this. Thanks!

onmousedown="hideElements([ 'cartview', 'other' ])"
and then:
function hideElements(what) {
alert(what[0]);
}

It looks like the real problem is that you're passing an string, not an array. So you'd do something like:
function hideElements(/* String */ what) {
alert(what.split(',')[0]);
}
or with an array:
function hideElements(/* Array<String> */ what) {
alert(what[0]);
}
or passing multiple strings directly into the function:
function hideElements(/* String */ what) {
alert(arguments[0]);
}

Related

How do I mix underscore functions to use a custom comparison algorithm for _.contains()?

Ideally, I want to pass a custom comparison function to _.contains() as a third argument, but it only accepts a collection and a value.
Code
I want to do this:
_.contains(['apples', 'oranges'], 'applesss', function (element, value) {
return new RegExp(element).test(value);
});
... but I can't, so what's the next best thing?
It sounds like you're looking for _.some, which returns true if the test passes for at least one element in the array:
_.some(['apples', 'oranges'], function (element) {
return new RegExp(element).test('applesss');
});
You can easily wrap it in your own function:
function test_regexes(arr, value) {
return _.some(arr, function (element) {
return new RegExp(element).test(value);
});
}
test_regexes(['apples', 'oranges'], 'applesss');

shorter syntax to bind multiple click events - javascript

Is there a shorter syntax to write this
$('.class1').click(function() {
some_function1();
});
$('.class2').click(function() {
some_function2();
});
$('.class3').click(function() {
some_function3();
});
$('.class4').click(function() {
some_function4();
});
knowing all the functions are different !
Since the functions does not seems to be accepting any special parameters, pass them as the callback itself
$('.class1').click(some_function1);
here is a proof of concept. Of course, you'd need to validate the regex return etc.. but its something to think about: Assuming the class will end in a number, and you have the corresponding function etc..
$('.class1, .class2').click(function() {
var num = 'some_function' + this.className.match(/(\d+)$/)[0];
test[num]();
});
var test = {
some_function1:function(){
console.log('in func 1 ');
},
some_function2:function(){
console.log('in func 2 ');
}
}
//param1: class name as string param2: function to execute
function binder(class, f){
$("'."+class+"'").click(f);
}
then call the function with your classes.

How to execute a JavaScript function when I send its name as string dynamically without parameters

I want execute JavaScript function which the name is coming as a string dynamically.
I don't need to pass any parameters while executing the function.
Please can any one guide me how to achieve this?
one simple way
eval("SomeFunction()");
or
var funcName = "SomeFunction";
var func == window[funcName];
func();
dangerous but you could use eval(method_name+"()")
are you talking about ´eval()´??
var foo = "alert('bar')";
eval(foo);
Hope this helps;
function a() {
console.log('yeah!');
}
var funcName = 'a'; // get function name
this[funcName]();
If the function is global, you should do window[funcName]() in browser.
Using eval is the worst way imaginable. Avoid that at all costs.
You can use window[functionname]() like this:
function myfunction() {
alert('test');
}
var functionname = 'myfunction';
window[functionname]();
This way you can optionally add arguments as well
Perhaps a safer way is to do something like this (pseudo code only here):
function executer(functionName)
{
if (functionName === "blammo")
{
blammo();
}
else if (functionName === "kapow")
{
kapow();
}
else // unrecognized function name
{
// do something.
}
You might use a switch statement for this (it seems like a better construct):
switch (functionName)
{
case "blammo":
blammo();
break;
case "kapow":
kapow();
break;
default:
// unrecognized function name.
}
You can optimize this by creating an array of function names, searching the array for the desired function name, then executing the value in the array.

javaScript jQuery function, how do i pass variables

What is wrong with my code. How do i pass attrfull to the inside. The way i have it done, if i run the function editsubmit($selected, size), $selected is inserted properly but i'm getting attrfull instead of size.
function editsubmit(attr, attrfull) {
if (attr.length) {
attr.val().length ? $selectedinput.attr({
attrfull: attr.val()
}) : $selectedinput.removeAttr(attrfull);
}
}
$selected is a variable and attrfull i a string. Do i need double qoutes around the string when i run the function like editsubmit($selected,'size').
Try
function editsubmit(attr, attrfull) {
if (attr.length) {
attr.val().length ? $selectedinput.attr(attrfull, attr.val()) : $selectedinput.removeAttr(attrfull);
}
}
Yes, you do need it to be a string (in double quotes), or else it will think you're trying to pass a variable reference.
The problem is this:
{attrfull: attr.val()}
I think you want it to be
{size: (whatever attr.val() is)}
So:
function editsubmit(attr, attrfull) {
if (attr.length) {
if (attr.val().length) {
var myObj = {};
myObj[attrfull] = attr.val();
$selectedinput.attr(myObj);
} else {
$selectedinput.removeAttr(attrfull);
}
}
}

Javascript using variable as array name

I have several arrays in Javascripts, e.g.
a_array[0] = "abc";
b_array[0] = "bcd";
c_array[0] = "cde";
I have a function which takes the array name.
function perform(array_name){
array_name = eval(array_name);
alert(array_name[0]);
}
perform("a_array");
perform("b_array");
perform("c_array");
Currently, I use eval() to do what I want.
Is there any method not to use eval() here?
You can either pass the array itself:
function perform(array) {
alert(array[0]);
}
perform(a_array);
Or access it over this:
function perform(array_name) {
alert(this[array_name][0]);
}
perform('a_array');
Instead of picking an array by eval'ing its name, store your arrays in an object:
all_arrays = {a:['abc'], b:['bcd'], c:['cde']};
function perform(array_name) {
alert(all_arrays[array_name][0]);
}
Why can't you just pass the array?
function perform(array){
alert(array[0]);
}
perform(a_array);
perform(b_array);
perform(c_array);
Or am I misunderstanding the question...
why don't you pass your array as your function argument?
function perform(arr){
alert(arr[0]);
}
I believe any variables you create are actually properties of the window object (I'm assuming since you used alert that this is running in a web browser). You can do this:
alert(window[array_name][0])

Categories

Resources