Jump to content

Recommended Posts

What is happening in the "name = { 'for..." statement?

 

What is happening here in the return statement?

 

 

function attr( elem, name, value ){
  if( !name || name.constructor != String ) return '';
  
  name = { 'for': 'htmlFor', 'class': 'className' }[name] || name;
  
  if( typeof value != 'undefined' ) {
    elem[name] = value;
    
    if( elem.setAttribute ){
      elem.setAttribute(name,value);
    }
  }
  
  return elem[name] || elem.getAttribute(name) || '';//here
}

 

A detailed explanation would be helpful, thanks.

Link to comment
https://forums.phpfreaks.com/topic/132508-solved-syntax-explanation/
Share on other sites

It's simply a JavaScript Object. I'm not sure what the [name] || name is for though.

 

But you declare objects in JavaScript like so:

 

var myObject = {
foo: 'bar', //A property
init: function(){ //A method
	alert(this.foo);
}
};
myObject.init();

 

Oh, duh, I should have realized it was an object just with different formatting. >.< thanks

 

As for the  expression || expression;.. I'm assuming it's casting to whichever is true, or the last expression if false, but I'm not certain.

Oh well, if its working then it should be fine. :P

Ha, not good enough for me >.< I need to know what it is, and why ~.~  Perhaps Nightslyr will stumble upon this topic and if he/she is not angry at me, enlighten me.

 

Thanks for the help thus far.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.