Friday, July 25, 2014

Objects in javascript

lets imagine we have a student what does this student have

  1. First name
  2. Last name
  3. address
  4. tel
  5. ......etc
all this each student have them so if you want to make a web page for school which has 100 students if we want to store the students varaibles we'll need for example 5 varaibles for each students so in total we'll have for example 500 varaible......
is that a good number of varabiles ?
cant we reduce this because this will make a problem for the memory or for the database but sure not for 500 varaibles but assume we have a big school of 2000 students!!! thats not acceptable at all
thats why they created Object.
What are Object?
objects is a variable with mutli properties and methods "functions" so for our school we can have one object but it'll have more than one section "just saying" so the student object will be like this:

where the normal variable only have one value 
in this way we'll have only 100 objects and this is more advanced way and more creative since we wont write the 500 variables 
so first name ,last name,.....etc are properties of that object and it has it own value.
Objects can have its own function so for example we can here put an fuction that calculate the Gpa of student so our object will be like this :
how to call or use one proprity of our object:
first of all we should know how to make a new object .
and you should know you can easily use the "var" to make an object lets see this example:

so our object have 3 proprites which is 
  1. first name
  2. last name
  3. address
Note: we didnt make a space in naming our variable because it'll be a syntax error but we can make it like this "first_name"

how to use it:

this will alert "Basel"....
or you can store it in another variable let see this:

and as we said objects can have methods "functions"

and dont worry we'll make an article of function in javascript so the object student will call the function name which will alert "Name".

 i hope i didnt make it long article 



No comments:

Post a Comment