Jump to content

[SOLVED] Syntax explanation


xtopolis

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 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.