1

i saw this code on navigation bar , first time i saw this type

var logoSH = fixIT ? 'show' : 'hide';
$('#mini-logo')

Ignore the

fixIT

It contains true or false

There is no. after $('#mini-logo') and first time I am seeing the hide() and show() method being called this way by hide.

How does it work?

1

2 Answers 2

2

In Javascript these are equivalent:

myObj.SomeProperty

myObj["SomeProperty"]

So this:

$("someSelector").hide();

is equivalent to:

$("someSelector")["hide"]();

The code you posted is equivalent to:

if (fixIT) {
     $('#mini-logo').show(300);
}
else {
     $('#mini-logo').hide(300);
}

But it's a little more compact and (IMHO) a lot less readable. Although, to be fair, it does ensure that the argument passed to .show or .hide (e.g. 300) is always the same which might be important to you. Also, if you had more than one argument to pass (and they were the same in both cases) it might be a little more useful too.

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

Comments

2

JavaScript has two member operators: dot notation and square bracket notation. Both these are used to access the members of an object.

The dot notation is used when you have a fixed property key to access, where as the square bracket notation is used when you have a key which is a key

When you say $('#mini-logo')[logoSH](300), assuming logoSH == 'show' it is equivalent to $('#mini-logo').show(300)

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.