Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Arrays and Objects in Javascript

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 127
    Comment on it

    Objects -> Javascript objects is a set of properties and methods . A method is a member of an object and their value is property . Javascript contain 4 kinds of objects :
    1) Intrinsic objects -> Array and String .
    2) Objects
    3) Host objects -> window and document .
    4) ActiveX objects .

    In Javascript object we can add and remove the methods at run time that is called Expando Property . These type of properties and methods can have any name and they can he identified by numbers like myobj.name , myobj.age etc .
    Example ->

    1. var obj = new Objects ( ) ;
    2. obj.name = " Mukesh " ;
    3. obj.age = 23 ;
    4. obj.getAge = function ( ) {
    5. return this.age ;
    6. } ;
    7. document.write ( obj.name ) ; // Output : Mukesh
    8. document.write ( obj.age ) ; // Output : 23
    9. document.write ( obj.getAge ( ) ) ; // Output : 23

    Arrays -> In Javascript all the things act as a Object so array is also behave as a Object or we can say arrays is also a special kind of object . Both Objects and Arrays can have properties and methods . Arrays have a length property but Object do not this is the one difference between Arrays and Objects .When you assign a value to an element of an array and if the index is greater than its length like myname [ 10] = " Mukesh " ; then the length property will automatically increased and length of an array will changed .
    Example ->

    1. var student = new Array ( 4 ) ;
    2. //Adding elements
    3. student[0] = " Mukesh " ;
    4. student[1] = 23 ;
    5. student[2] = " Male " ;
    6. student[3] = new Date ( 2000 , 1, 1 ) ;
    7. document.write( " Length is : " + student.length ) ; // Output is : 3
    8. //Add some Expando properties
    9. student.expando = " Javascript " ;
    10. student[ " Second Expando " ] = " Tutorial " ;
    11. document.write( " New Length is : " + student.length ) ; // Output is : 3

    Multi - Dimensional Arrays
    In Javascript multi dimensional arrays is different to other languages , it does not support directly multi dimensional array in javascript but you can get the behaviour of multi dimensional arrays by storing arrays within the elements of another array . For example

    1. var size = 3 ;
    2. var a , b ;
    3. var multiply = new Array ( size + 1 ) ; //set the length of the array to size + 1 and first index would be 1 not 0
    4. // Create the columns in the table
    5. for ( i =1 ; i < = size ; i++ ) {
    6. Multiple[i] = new Array ( size + 1 ) ;
    7. // Fill the row after the multiplication
    8. for ( j =1 ; j < = size ; j++)
    9. {
    10. Multiply[ i ][ j ] = i * j ;
    11. }
    12. }
    13. document.write ( Multiply[ 2 ][ 3 ] ) ; // Output -> 6
    14. document.write ( Multiply[ 4 ][ 5 ] ) ; // Output -> 20
    15. document.write ( Multiply[ 1 ][ 8 ] ) ; // Output -> 8

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: