trenccan
trenccan

Reputation: 720

Is this line of code jQuery?

I'm checking some code and found this row:

section0.top = $("#section0").position().top;

What is section0.top - is it a variable? I didn't see anywhere in jQuery documentation that I could put .top after the variable. I don't understand this syntax.

This is the whole function:

function calculateSections() {
    windowTop = $(window).scrollTop();
    section0.top = $("#section0").position().top;
}

Upvotes: 0

Views: 40

Answers (2)

trincot
trincot

Reputation: 350310

In HTML5 global variables are created automatically for each element that has an id attribute, and those variable names are the id values.

So without any explicit declaration your document will have the section0 variable, and its value is the corresponding HtmlElement object. The assignment you refer to will thus add a top property to that object.

NB: this is behaviour that is unrelated to jQuery

NB2: it is not considered best practice to add properties to DOM objects.

Upvotes: 2

Javier Cobos
Javier Cobos

Reputation: 1182

According to this function documentation:

https://api.jquery.com/position/

Returns an object containing the properties top and left.

It means that position() over a selector is a jQuery function but at the end its just returning a javascript object in this format:

{top: 0, left: 0}

It menas that you can add a chainable access to the property, in this case top

Note that position() is not a jQuery variable but a method.

Upvotes: 0

Related Questions