Chapter 4. JavaScript Functions and Objects: Serious JavaScript

  • You define a function with parameters, you call a function with arguments.
  • If a variable is declared outside a function, it’s GLOBAL. If it’s declared inside a function, it’s LOCAL.
  • Globals live as long as the page.
  • Local variables typically disappear when your function ends.
  • use globals where it makes sense, but use them in moderation, and whenever possible, make your variables local.
  • To create a function, use the function keyword with parentheses to hold parameters, if there are any.
  • Functions can be named, or be anonymous.
  • The body of a function goes between curly braces{}, and contains statements that do the work of the function.
  • JavaScript uses pass-by-value parameter passing.
  • Functions are values that can be assigned to variables, passed to other functions, stored in arrays, and assigned to object properties.
  • Objects are collections of properties.
  • You access an object’s properties using dot notation or the [ ] notation.
  • If you use [ ] notation, enclose the property’s name as a string; for example, myObject[“name”].
  • A function assigned to an object property is referred to as a method.
  • A method can use a special keyword, this, to refer to the object on which it was invoked.
  • A constructor is a function that makes objects.
  • To invoke a constructor to create an object, use the new keyword. For example, new Dog().
  • We’ve already been using several objects in this book, including document, window, and various element objects.
  • The window object is the global object.
  • The document object is one of window’s properties.
  • The document.getElementById method returns an element object.

var sound = bark("Fido", 50) ;
alert(sound);
function bark(dogName, dogWeight) {
    if (dogWeight <= 10) {
        return dogName + " says Yip";
    } else {
    return dogName + " says Woof";
    }
}    


<script>
var banzaiMovie = new Movie("Buckaroo Banzai",
								"Cult Classic",
								5,
								["1:00pm", "5:00pm", "7:00pm", "11:00pm"]);

	var plan9Movie = new Movie("Plan 9 from Outer Space",
								"Cult Classic",
								2,
								["3:00pm", "7:00pm", "11:00pm"]);

	var forbiddenPlanetMovie = new Movie("Forbidden Planet",
										"Classic Sci-fi",
										5,
										["5:00pm", "9:00pm"]);

alert(banzaiMovie.getNextShowing());
alert(plan9Movie.getNextShowing());
alert(forbiddenPlanetMovie.getNextShowing());

function Movie(title, genre, rating, showtimes) {
    this.title = title;
    this.genre = genre;
    this.rating = rating;
    this.showtimes = showtimes;
    this.getNextShowing = function() {
        var now = new Date().getTime();
        for (var i = 0; i < this.showtimes.length; i++) {
            var showtime = getTimeFromString(this.showtimes[i]);
            if ((showtime - now) > 0) {
                return "Next showing of " + this.title + " is " + this.showtimes[i];
           }
        }
    };
}


    var movie1 = {
        title: "Plan 9 from Outer Space" ,
        genre: "cult classic" ,
        rating: 2 ,
        showtimes: ["3:00pm", "7:00pm", "11:00pm"],
        getNextShowing: function(){
        var now = new Date().getTime();
	    for (var i = 0; i < this.showtimes.length; i++) {
		var showtime = getTimeFromString(this.showtimes[i]);
		if ((showtime - now) > 0) {
			return "Next showing of " + this.title + " is " + this.showtimes[i];
		}
	}
	return null;
    }
};

     var movie2 = {
        title: "Forbidden Planet" ,        
        genre: "classic sci-fi" ,
        rating: 5 ,
        showtimes: ["5:00pm", "9:00pm"] ,
        getNextShowing: function(){
    var now = new Date().getTime();
	for (var i = 0; i < this.showtimes.length; i++) {
		var showtime = getTimeFromString(this.showtimes[i]);
		if ((showtime - now) > 0) {
			return "Next showing of " + this.title + " is " + this.showtimes[i];
		}
	}
	return null;
    }
}; 

function getNextShowing(movie) {
	var now = new Date().getTime();
	for (var i = 0; i < movie.showtimes.length; i++) {
		var showtime = getTimeFromString(movie.showtimes[i]);
		if ((showtime - now) > 0) {
			return "Next showing of " + movie.title + " is " + movie.showtimes[i];
		}
	}
	return null;
}


    function getTimeFromString(str) {
	var theTime = new Date();
	var time = str.match(/(\d+)(?::(\d\d))?\s*(p?)/);
	theTime.setHours( parseInt(time[1]) + (time[3] ? 12 : 0) );
	theTime.setMinutes( parseInt(time[2]) || 0 );
	return theTime.getTime();
    }

</script>


Word list

  • trace
  • clause
  • chock-full
  • function(函数)
  • enumerate
  • Parameter(形参) = 定义函数时的占位符
  • Argument(实参) = 调用函数时传入的实际值
// name 是 parameter(形参)
function greet(name) {
  console.log("你好, " + name);
}

// "小明" 是 argument(实参)
greet("小明");

<
Previous Post
1021 Winter is coming
>
Next Post
1022 Reading and learning as usual, and added badges to About Me