Difference between a property and a method?

Hello , one basic question :
console.log(“Hello”.length)
.log is a method
.length is a properties

So properties = method ?
else what the difference between ?

A method is an attribute, but that does not make an attribute a method. A method is function, so performs some task. .length is a value, only.

We poll values, and call (or invoke) methods.

31 Likes

Follow up question. I’ve seen that “accessors” inside classes can be used as properties. For example,

var book = {
  pageCount: 314,
  get length() {
    return this.pageCount;
  }
};

And this can be used as book.length;

Here, is length a method or a property or both?

2 Likes

It is an accessor property (as opposed to data property), but also a special method. As written above it is a little bit deceptive, though, since .length is a property of iterable objects such as arrays and strings. If a reader sees this, book.length they might be led to believe that book is one of those, and not a plain object (object.length => undefined).

Bottom line, the language may let us do a lot of things, but some of them are better left in the drawer.

6 Likes

What is an attribute?

In JS we call them properties.

object = {
    property_name: 'property_value'    //  property (attribute)
}
1 Like

How does the object keyword console makes and transmit data to the second console black window pane?

Keywords are the same as functions/methods?

The console is the standard output from the editor window, using the console.log() method. The objects don’t have any relation to the JS console.

Not the same, but many of them are built-in functions, others are built-in constructs.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar

2 Likes

This course teach constructs?

Things like if statements, for statements, while statements, switch statements, etc. are constructs, not functions. Many of the keywords are expressions or operators, such as, in, of, typeof, etc.

1 Like

Property is the same as a variable?

Similar, but not the same. Variables have scope, properties have object context. When you get into class and constructors you’ll see that object properties are called instance variables, which is where they are similar.

a = 42
b = {
    a: 42
}

a and b are variables, b.a identifies the a property of b.

3 Likes

You are awesome mtf!

You are really good at explaining!

Thanks from Denmark!

3 Likes

Sorry, but can you explain in layman terms what scope and object context is? Also, if an object is a collection of data, isn’t everything an object? For example, variable have data stored in it and properties are pieces of data about an object

Scope is the name we use to describe the environment where variables are accessible for read and write operations. There are two such environments, global and block. The latter could be a function, an if statement, a for or while loop, a switch or anything with a parameter and a body. Eg. (){}

When we are inside a block either within a function, or in global scope, the body has its own private scope when variables are declared with let or const. They cannot be accessed from outside that body, and neither do they persist when the code body has finished executing.

Global scope is the main execution context to which all undeclared variables are assigned. Regardless where they are defined they belong to the global scope and are accessible everywhere.

Before we mess this up too much, perhaps a quick read of the docs will shed more light…

https://developer.mozilla.org/en-US/docs/Glossary/Scope

When we account for the hierarchy of the scope chain we can pretend we have rooms within rooms, and all having a two-mirror that permits each of them to see outside, all the way to the outermost zone, but nothing can see the other way. Scope is like that. Variables and functions that are written in outer scope are accessible in inner scope. We call that looking up the scope chain.

When discussing variables and functions we generally refer to their scope; however, when discussing objects we refer to their context. They are overlapping terms that can have similar meaning, but also dissimilar meanings. Technically, even functions are properties of some parent object, be it window or otherwise defined object. The object is its own execution context.

Seeing how easy it is to blur the lines, it’s safe to generalize that scope and context are very much alike, the same thing, so to speak. How we discuss them is along the lines of the code we are discussing.

If the code is largely made up of global functions and variables, we talk scope. If the code is object or class based we talk context with properties and methods. It’s really just a matter of perspective.

Thanks for the explanation! It sounds foreign, but I can kind of understand what you’re saying. By the way ,the docs gave the definiton of “the context of execution” for scope, but could you clarify what "context of execution means? Also, is everything either a global scope or a block scope, or can they be both. And if they are both, are all block scopes made within a global scope?

Sorry for asking so many questions, but I hear the word ‘context’ a lot when reading definitions. Can you explain what context is relating to computer languages?

Last questions - I promise. Are objects and functions (or in this case, methods) similar? And are instances similar to examples of data types?

Context describes the scope within an object. Methods (that is functions that are properties of an object) can only be accessed by object reference, as in,

foo = {
    bar: 'bar',
    print () {
        console.log(this.bar)
    }
}

foo.print()    //  bar

We cannot access print() from outside of the execution context of the object. The context is indicated by the this property. The method is able to access it because it is up the scope chain from the method.

Methods are functions but they do not exist in global scope, only in object context.

To give an idea of what an instance is we need to define a class.

class Shape {
    constructor (locus) {
        this.locus = locus
    }
    print () {
        console.log(this.locus)
    }
}

circle = new Shape('circle')    //  an instance of the Shape class

circle.print()    //  circle

We can use the .constructor.name property to see what data type this is…

console.log(circle.constructor.name)    //  Shape