I think I understand it better now, thank you for your explanation sir.
Didn’t really understand what is meant by instances, like do they mean literal instaances in time or is describing something else?
class Foo {
}
foo = new Foo()
Above, foo
is an instance of the Foo class. An instance is an object.
An instance is nothing but an object of a class.
class xyx{
xyz obj = new xyz();
}
Here, obj is an instance of class xyz.
Hmmm…
class Xyz {
}
obj = new Xyz();
console.log(obj instanceof Xyz) // true
@namibianwolf it’s not really the same kind of instance as you’d assume in physics. When you learn about classes you’d understand better. For now, let me break it down for you:
Imagine I give you rice. You know rice is a type of food.
Then, if I give you beans, you also know beans are a type of food.
So in programming, we’ll say: rice and beans are both instances of food!
When we talk about methods and properties, think about it like this:
what could be the properties of the rice I gave you? It might be white!
Also, the beans could be brown. Therefore we can say the food class has a “color” property which enabled us to know the color of the rice and the beans.
Now let’s talk about methods.
Methods are sort of operations we can carry out on instances of a class.
So with the rice and beans from our food class, what operation can we carry out?
We can EAT it! Therefore: EAT is a method of food class!
Okay, stop salivating now, back to coding…hehehe.
We have various datatypes, such as strings, numbers, booleans, etc.
So if I give you 50, it is a number, so it is stored as an instance of number datatype.
If i give you “FC Barcelona”, it is a string, so it is stored as an instance of string datatype.
I hope I was able to help
THANK YOU! It’s hard to understand and learn these new things in some of the forums by answering with even more information we’ve yet to come across and understand.
You made this simple. It’s just foreign, so understanding is… much needed. Now for some rice, beans, and tacos. Feel free to add more food references in the future
When I am finished with the course (and understand to where I can THINK as a coder), I hope to help in the same way.
This is the best explanation for a non-IT background guy like me…
Rice or beans, we’re the cook. What escapes learners is control. We are in control. There is no body out there taking it away.
Tell me about Object. I’m not getting what an object actually is…
An object literal is a form of data composed of a block, {}
, and containing one or more ‘key: value’ pairs called properties.
object = {
key: value // property
}
That’s the literal form. In more general terms, everything in JS is an object (although it may not look like a literal).
1 => 'number' type, Number object instance
'a' => 'string' type, String object instance
true => 'boolean' type, Boolean object instance
function () {} => 'function' type, Function object instance
{} => 'object' type, Object object instance
[] => 'object' type, Array object instance
null => 'object' type
For now focus on learning the syntax, how to write an object literal and how to poll the object for the values it contains. More detail will filter in over the duration of the course.
- You said that
1
is aNumber object instance
. From what I understood, isn’t1
an object itself? So wouldn’t 1 be aNumber class instance
?
- So if I understood correctly, a
class
is like the definition and aninstance
gives the properties some values. So aclass
calledPerson
could have manyproperties
such asname
,age
,height
,weight
, etc. That is basically the definition of a person. A person has a name, age, height and weight. Then, aninstance
(E.g. Messi) would give eachproperty
avalue
. It would set itsname
property to “Lionel Messi”, hisage
property to 33, hisheight
property to “1.7m” and hisweight
property to “72Kg”
Class, object, same thing, really.
Every string instance has a property called
length
that stores the number of characters in that string - Codecademy Introduction to JavaScript Lesson 7
"Hello, World!"
is a string. However, it is not an instance of the String
class. It is a primitive. How come it has the length
property? And can you please define the word primitive? I don’t think I totally get it
Primitives are not instances of a class, but they have a wrapper that is given to them by JS based on the recognized type. The wrapper object is what has the length attribute. This is vague, I know. Will need to do some digging to come up with a better explanation.
Not sure there is that great of detail in the course, being more focused on syntax, constructs and basic usage. The technical reading can be rather dry and distracting.
Edit: 1 SEP 21: removed unbound link
Thank you for an article recommendation. Do you think I should read this article now or should I wait until I have a basic understanding of objects? I am currently on properties
You’re welcome. It’s a brief article, and not very dense. Five minute read.
Great! I’ll check it out tomorrow. I spent nearly 3 - 5 hours on properties