Difference between a property and a method?

@mtf When you say that scope is the environment where variables can be used for read and write operations, what are read and write operations? And are there any environments where code can’t be used for read and write

A write operation would be when a variable is defined by the assignment of a value, or redefined/updated if it already exists.

A read operation is only a matter of polling a given variable or data structure by reference.

Seems it would be counterproductive, in the main. The whole premise of a program is the movement and dissemination of data.

As to where we are allowed to read from and write to variables, well, that will be its scope. Where it is determines that. A virtual Russian Doll, as it were. The very innermost have access to everything above them; and, the very outermost have access to nothing within.

Could you explain what polling is?

Polling is the act of referencing data.

let a = 42

The second line is a function which argument polls a variable.

Oh I see, would console.log("string".length); be a poll?

Yes. We are polling the length property of the string object.

Polling is a direct request of the environment to deliver up a value. The interpreter will take that to mean go all the way to the top to satisfy this request, else respond, undefined. We can poll from anywhere, but must expect what we poll to exist in some local or higher scope. We are not mutating anything, simply asking for some value in response.

The counterpart, though one would not consider it an opposite, as such, is a query which doesn’t reference anything in particular but rather seeks a match with an object somewhere in the wild; i.e., the sample.

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a.filter(x => ! (x % 2))

The above is a query of the a array for values that meet the condition given in the filter callback. We would expect all the values in b to be even parity.

Equate the term, query to the term, match. A query is not a pollable thing since we have no reference to begin with. Hence, we query our data structure for possible matches. Those matches can then be made pollable.

Even though query and polling are opposites, couldn’t you interpret query as a response of a request to match something? (Also, what does you’re second line do?)

You could, indeed. A query only returns matches or near matches.


It may mean jumping ahead of where you are in the learning path, so count this as something of a sneak peek…

You will learn about a special type of function called an iterator. The name refers to methods that accommodate a function known as a callback which the iterator will call upon as it iterates its context object.

context.iterator(function (x) {
    // code to execute on each iteration

Glossing over that, the line of code you ask about is acting on the context of object, a with the Array.filter() method that takes a predicate function as its argument. The net effect is to return an array of even numbers. It is an example of matching data to criteria so that only matches are returned. It’s a slightly different form of query, but a query none the less.

What’s a callback? And I don’t really understand what you’re definition of iteration is, but would an iteration be the same as a while or for loop? (I’m not sure those things exist in Javascript, but it does in java and python) Also, since a query is a response to a request, couldn’t you define it as a poll?

A request is only a poll if the request can be handled. 404 is the response when a page cannot be found, hence more like querying than polling. It’s semantics, and not something to mull over. If you see the word poll, then assume it to mean accessing a variable for a value we know to exist.

A for callback, that is going to come up later in the topic of functions that take other functions as their arguments. Let this one go for the time.

Iteration is what we do in loops that that take in one value at a time from a set or sequence, such as a string, an array, or an object. In the case of sets, arrays and strings, those items are found by index; and, in the case of objects, those items, or entry values are found by their key.

Keep working with the loops you are learning and get in lots of practice. When it comes to the unit on iterators, you will have the necessary tools to write loops that do exactly what they do. We’re sure to come up with more questions by that time so just ask away when your reach it.

okay, consider a person. She have some properties like her hair color, height, face structure.
And then she can perform some tasks like she can speak, she can walk, she fight etc. These are the methods.
Similar is the thing in JS

so is it safe to say properties gives information and methods modifies code, where as properties don’t modify code but tells you about a value/or attribute ?

All methods are properties, but not all properties are methods. Properties are either data or behavior, which behavior is the methods that can operate on the data of the current instance, or owner object. Methods do not modify code. They may modify data, or simply retrieve it.

Is console.log a method or just .log? Also, could you explain why or why not?

console is an object, .log() is a method of that object.

1 Like