Intro into JavaScript objects

Joined
Feb 15, 2021
Messages
99
Reaction score
0
i am just beginning to understand "objects"

what is an object?

what can i do with an object?

how do i code with objects?

anything else i should know :)

go slow and simple for me

thank you
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
As really old books says: For example an Apple is an Object. It has some features like color:green, camefrom:tree and so on and so on.

In Javascript actually almost everything i an object. Even HTML Elements are objects.

Example:
Code:
<div id="myDiv">My Text</div>
<script>
var myDiv=document.getElementById('myDiv');
console.log(myDiv);
console.log(myDiv.innerHTML);
console.log(myDiv.id);
</script>
Try it and see what your browser console says.

But of course you also may create an Object.

Code:
<script>
var myObj={fruit:'Apple',color:'green',gravitation:function(){var o=this; console.log(this.fruit+' color is '+this.color+' falls from the tree');}}
for(var i in myObj){
console.log(i+' = '+myObj[i]);
}
myObj.gravitation():
</script>
Try it and see what your browser console says. Play along those values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top