Distinguishing beginner definitions in relation to examples (Method, Instance + Library, Object, String)

Hi, I’m simply trying to better relate, distinguish, and understand some general beginner terms in relation to the very first example here:

INTRODUCTION TO JAVASCRIPT

In regards to the definitions that I have read here:

JavaScript Syntax

Example:

console.log(31)


"METHOD"

Is “.log()” the method (action for the object “console” written before it?), and basically anything appending an instance with a dot operator + method name + parentheses is considered a method?

In the above example, the method puts to action the logging/printing inside the parenthesis (31) which we will then see in our console, correct?


"OBJECT"
Is anything in a line of code that has value or instructions considered an object?

In the above example, is “console” considered an object because it has internal data (instructions/is a Javascript keyword)?

And would “(34)” also be considered an object due to representing value?

I’m trying to understand if object is an interchangeable or perhaps synonymous label.


"INSTANCE" and "LIBRARY

“instance” has me confused when I read the “method” definition in:

It’s definition:

“Methods return information about an object, and are called by appending an instance with a period ., the method name, and parentheses.”

It’s example:

// Returns a number between 0 and 1
Math.random();

Since it says that you append an instance with a period etc., this tells me that it’s referring to “Math” as the instance.

So, is “Math” an object that is an instance, and is an instance because it is unique among a class of arithmetic that “Math” falls under?

In the same glossary below the “method” definition with the same example:

// Returns a number between 0 and 1
Math.random();

It defines Library as containing methods, while pointing out that “Math” is the library.

Not sure what I am asking here other than pointing out that “Math” seems to be referred to as an instance, an object, and a library. Please correct me if I am wrong.


String

Lastly, does “string” just refer to anything contained within the () parentheses of the method?


Sorry for the wall of text, I’m just trying to be as descriptive as possible in my confusion. I’ve gone further in the lessons but am reversing course to better understand the fundamental definitions and how they are applied and distinguished from each other in the examples.

Thank you so much.

2 Likes

Hi @geraldlockerby and welcome to the forum!

Some good questions here. Especially since Javascript is considered a bit like the anarchist among the programming languages when it comes to type security.

Yes, I think that describes it pretty good.

This is a bit trickier:
Javascript treats almost everything as an object. But some data types are considered primitives rather than objects. A number for example. Or a string. Yet you can apply methods to strings. Like .split() for example. I don’t know what you mean by ‘instructions’ though.

In this example 34 is called an argument for the method .log(). But what you can pass as an argument depends on the method. That could be anything from a function to an object.

In the following courses, you will create your own objects and write your own methods for these objects. But there is a built in preset of objects and methods related to these objects like the console object and the method .log().

Methods are basically functions restricted to an object.

In the above example, these would be the terms:

      method 
         ↓
console.log(34);
   ↑         ↑
 object   argument (typeof number)

This is what an object declaration would look like:

const myObj = {
 myMethod(arg){
  return `This was the argument passed: ${arg}`
 }
}
console.log(myObj.myMethod('myargument')); // logs "This was the argument passed: myargument"

Somewhere in the Javascript core, the console was defined in a similar way.

This confuses me too, to be honest: Objects usually have a constructor property. Not so the Math object. This is what the MDN docs say:

Unlike many other global objects, Math is not a constructor. All properties and methods of Math are static.

@stetim94 can you answer that?

As already mentioned above, a string is a primitive data type within quote marks.

const aString = 'anything written inside quotemarks'. 

Yes. The object instance bears the name console, and .log() is one of the inherited methods.

An instance is any object that owes its existence to a constructor function, or class. In JS pretty much everything inherits from one constructor or class or another. The only object with zero inheritance is null. But it does have a type, 'object'. It is the only object that we cannot instantiate.

When we write a string literal, it is primitive, also without a constructor or class. It lays in memory in its character string form, and assuming it has been assigned to something, can be referred by that something. It is in the process of retrieval of that reference that JS jumps into action and classifies the primitive according to its own built in set of rules for said classification, namely, does it have quotes on either end? If so then it should be given to the String constructor to make it into an actual string instance. Now it will have a type, 'string'. It has been instantiated as a String object and as such now inherited all the methods of the parent constructor (class).

The same holds for number primitives. They do not have a type until JS determines which wrapper rule they fit into. It only gets a type when the number is retrieved, as above. In memory it remains a primitive.

print (42 instanceof Number)
false

JS does all kinds of object wrapping in the background, but it does not declare them as instances. Only we can instantiate an object.

print (new Number(42) instanceof Number)
true

Now the object we’re querying is an instance of the Number constructor.

The main reason JS is so slow is loose typing and all the background work that must be done when working with raw data. The type is only known once the data is under the microscope. That takes a lot of behind the scenes scrutiny. Not our concern, here, though, so forgive that I digress.

We free up JS by having our own defined data types which the language supports very well. Write the constructor and spin off instances, then use those objects, along with their inherited methods in our processes. In JS this is known as prototyping. Every object inherits from the prototype of the parent object, and any parent from which that object inherits.

1 Like

Thanks for your detailed information about the JS background activity for incoming data types @mtf.
Some of your explanations raise new questions to me. I’m a bit insecure though whether this thread is the right context for that or it should rather be splitted.

How would this fit in with talking about Math in our code as an instance?

A previous assignment of a string to a variable doesn’t seem to be necessary for a string to make use of object methods as this also works:

console.log('string'.split('i')); // ["str", "ng"]
console.log('string'.length); // 6

What is processed here: the string wrapped as an object by JS in the background prior to the .length property beeing interpreted or the primitive?

The primitive has no properties. For the purpose of computing the length, it would be given a wrapper object,

{
  value: 'string',
  length: 6
}

Of course it’s not all as simple as that, but what do we care? JS does all that on the fly, in the background using built in methods (Object.assignProperty?) and the String constructor.

The language helps us with the details so long as our syntax is correct is all we need know. Once we learn the various methods that apply to object instances our toolbox is adequately outfitted.

As for the Math object, to see that it is not a constructor, just try it out…

math = new Math()

TypeError: Math is not a constructor

I did. We do not instanciate it. That’s why I wonder why we call it an instance…

Because it still has a parent constructor, Object, which prototype it inherits from.