Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Empty Function Paramaters
Old 01-27-2009, 07:59 PM Empty Function Paramaters
Extreme Talker

Posts: 196
Trades: 0
How do i declare a default value in a function? I have a simple function

Code:
function color_input(id,token){

    if (!empty(token)){
        document.getElementById(id).style.backgroundColor = '#2A2A2A';
        document.getElementById(id).style.color = '#DDC58C';
    }else{
        document.getElementById(id).style.backgroundColor = '';
        document.getElementById(id).style.color = '';
    }

}
Called in HTML like

HTML Code:
<input type="text" id="example_name" onblur="color_input('example_name');">
You will notice that the #2 parameter is missing in the html call. Sometimes i don't have it to send. in PHP i would just set the functions second param like token=''

Code:
function color_input(id,token=''){}
How do i set a default param in a JS function?
empiresolutions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-27-2009, 08:11 PM Re: Empty Function Paramaters
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
function name(p_Para1, p_Para2) {
if (p_Para1 == null) {
    p_Para1 = defaultval1;
}
if (p_Para2 == null) {
    p_Para2 = defaultval2;
}
// rest of function
}
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-28-2009, 08:33 AM Re: Empty Function Paramaters
Extreme Talker

Posts: 196
Trades: 0
@chrishirst - thank much.

Also i found i can add this to my function.
Code:
if (typeof token != 'undefined')
empiresolutions is offline
Reply With Quote
View Public Profile
 
Old 01-28-2009, 08:51 AM Re: Empty Function Paramaters
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Here is a more elegant solution.

The reason this works is that the logical OR of JavaScript returns values in left to right precedence when assigning values. You can simply do something like this (I'm using a more extreme example where the input is a whole object with several properties):

Code:
function myFunction(obj) {
var defaults = {
     prop1: "hello",
     prop2: "world",
     prop3: "goodbye",
     prop4: "world"
}
//the magic
obj = obj || defaults;
//logic goes here...
}
Of course, this will work with any value, not just a set of objects. Set a default then say {variable = variable || default} where variable is the function input.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 01-28-2009 at 12:32 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to Empty Function Paramaters
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.88635 seconds with 12 queries