Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of passing an empty object to jQuery?

Tags:

jquery

What does $({}) in jQuery mean ? I saw this on victmo response on this question : Possible to fade out div border?

He used $({alpha:1}).animate({alpha:0}) how this is affecting the DOM and what happens literally?

like image 759
Tarida George Avatar asked Sep 04 '14 15:09

Tarida George


People also ask

Is Empty object jQuery?

jQuery isEmptyObject() methodThe isEmptyObject() method is used to determine whether the passed argument is an empty object or not. It returns a Boolean value. If it finds the passed value is an empty object, it returns true. Otherwise, it returns false.

How do you clear an object in jQuery?

The empty() method removes all child nodes and content from the selected elements. Note: This method does not remove the element itself, or its attributes. Tip: To remove the elements without removing data and events, use the detach() method. Tip: To remove the elements and its data and events, use the remove() method.

How to use empty function in JavaScript?

The empty statement is a semicolon ( ; ) indicating that no statement will be executed, even if JavaScript syntax requires one. The opposite behavior, where you want multiple statements, but JavaScript only allows a single one, is possible using a block statement, which combines several statements into a single one.


1 Answers

What does $({ }) in jQuery mean ?

It means "pass an empty object to jQuery and create a jQuery object from it".

how this is affecting the DOM and what happens literally?

It doesn't affect the DOM at all, it simply changes the property value of the object {alpha:1} over time.

You can use some jQuery methods on plain objects as explained in the documentation. Although it seems to be a bit outdated since animate is not listed there. But it works indeed:

> $({alpha:1}).animate({alpha:0}, {step: function() { console.log(this.alpha); }})
1
0.9965342284774632
0.9870866934849247
0.9730426794137726
0.9524135262330098
0.9242551074907518
0.8926584654403724
0.8563192594626027
...
like image 140
Felix Kling Avatar answered Sep 17 '22 01:09

Felix Kling