04 JavaScript Basis 2

1. Function Statements and Expressions

  • Function Declariation
  • //function declariation
    function whatDoyouDo(job, firstName)
    {
          
          	
    	//...
    }
    
    whatDoyouDo('teacher',John);
    
  • function expression
  • //function expression
    var whatDoyouDo = function(job, firstName)
    {
          
          
    	//...
    };
    
    whatDoyouDo('teacher',John);
    

    2. Array

  • Initialise new array
  • var names = ['John', 'Mike', 'Jane'];
    
    var years = new Array(1990,1969,1948);
    
  • Length of array
  • console.log(years.length);
    
  • Mutate array data
  • names[1] = 'Ben';
    names[names.length] = 'Mary';
    names[8] = 'Kim';
    
  • Different data types
  • var john = ['John', 1999, 'happy', false];
    
  • Method
  • john.push('bule');  //Add a new element to the end
    john.unshift('Mr'); //Add a new element to the beginning
    john.pop(); //Remove the last element
    john.indexOf(1999); //Find the position of the element
    

    3. Objects

  • Difference between Array and Objects: Array has order but objects don't.
  • Initialise new Object
  • //Object literal
    var john = {
          
          
    	firstName: 'John',
    	lastName: 'Smith',
    	birthYear: 1990,
    	family: ['Jame', 'mark', 'Bob', 'Emily'],
    	job: 'teacher',
    	isMarried: false
    };
    
    //new Object syntax
    var jane = new Object();
    jane.name = 'Jane';
    jane.birthYear = 1998;
    jane['lastName'] = 'Smith';
    
  • Method
  • //Object literal
    var john = {
          
          
    	firstName: 'John',
    	lastName: 'Smith',
    	birthYear: 1990,
    	family: ['Jame', 'mark', 'Bob', 'Emily'],
    	job: 'teacher',
    	isMarried: false,
    	calAge: function()
    	{
          
          
    		this.age =  2019 - this.birthYear;
    	}
    };
    john.calAge();
    console.log(john);
    

    4. Hoisting

  • Declaration works with variables and functions
  • //works fine VARIABLES
    num+=6;
    var num;
    
    //works fine FUNCTION
    calculateAge(1997);
    function calculateAge(year)
    {
          
          
    	console.log(2019-year);
    }
    
  • Expression works fine with variables but does NOT work with functions
  • //work fine
    num+=6;
    var num = 0;
    
    //cannot work FUNCTION
    calculateAge(1997);
    var calculateAge = function (year)
    {
          
          
    	console.log(2019-year);
    }
    

    5. Scoping

    var a = 'Hello!';
    first();
    
    function first() {
          
          
        var b = 'Hi!';
        second();
    
        function second() {
          
          
            var c = 'Hey!';
            console.log(a + b + c);
        }
    }
    //input: Hello!Hi!Hey!
    
    var a = 'Hello!';
    first();
    
    function first() {
          
          
        var b = 'Hi!';
        second();
    
        function second() {
          
          
            var c = 'Hey!';
            third();
        }
    }
    
    function third() {
          
          
        var d = 'John';
        console.log(a + b + c + d);
    }
    //input: Uncaught ReferenceError: b is not defined
    

猜你喜欢

转载自blog.csdn.net/flavioy/article/details/100547020