The environment in which the JavaScript code runs and is interpreted
by a JavaScript engine.The runtime provides the host objects that
JavaScript can operate on and work with.
The JavaScript runtime is the “existing entity or system” mentioned
in the scripting language definition. Code passes through the JavaScript
engine, and once parsed and understood, an entity or system performs the
interpreted actions. A dog walks, a person runs, a video game character
jumps (or in the case of the above image, wrecks).
Applications make themselves available to JavaScript scripting by
providing “host objects” at runtime. For the client side, the JavaScript
runtime would be the web browser, where host objects like windows and
HTML documents are made available for manipulation. Have you ever worked
with the window or document host objects? The window and document
objects are not actually a part of the core JavaScript language. They
are Web APIs, objects provided by a browser acting as JavaScript’s host
environment. For the server side, the JavaScript runtime is Node.js.
Server-related host objects such as the file system, processes, and
requests are provided in Node.js.
n interesting point: different JavaScript runtimes can share the same
JavaScript engine. V8, for example, is the JavaScript engine used in
both Google Chrome and Node.js — two very different environments.
if (scoops >= 5) {
alert("Eat faster, the ice cream is going to melt!");
} else if (scoops < 3) {
alert("Ice cream is running low!");
} else { alert("Still lots of ice cream left, come and get it."); }
var justAVar = "Oh, don't you worry about it, I'm GLOBAL"; function whereAreYou() { var justAVar = "Just an every day LOCAL"; return justAVar; }
var result = whereAreYou(); console.log(result);
#显示Just an every day LOCAL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#实例2
var justAVar = "Oh, don't you worry about it, I'm GLOBAL"; function whereAreYou() { var justAVar = "Just an every day LOCAL"; function inner() { return justAVar; } return inner(); }
var result = whereAreYou(); console.log(result); #显示Just an every day LOCAL
var justAVar = "Oh, don't you worry about it, I'm GLOBAL";
function whereAreYou() { var justAVar = "Just an every day LOCAL"; function inner() { return justAVar; } return inner; } var innerFunction = whereAreYou(); var result = innerFunction(); console.log(result); #显示Just an every day LOCAL
var allCanFly = processPassengers(passengers, checkNoFlyList); if (!allCanFly) {
console.log("The plane can't take off: we have a passenger on the no-fly-list."); } #checkNoFlyList即传入的函数
从函数返回函数
1 2 3 4 5 6 7 8 9 10 11 12 13
function createDrinkOrder(passenger) { var orderFunction;
if (passenger.ticket === "firstclass") { orderFunction = function() { alert("Would you like a cocktail or wine?"); }; } else { orderFunction = function() { alert("Your choice is cola or water."); };
}
return orderFunction; }
匿名函数(anonymous functions)
匿名函数是没有名称的函数表达式,用于通常需要函数引用的地方。
window.onload = function() { alert("Yeah, that page loaded!"); };
如果需要将一个函数作为实参,可将实参指定为函数表达式
1 2 3
setTimeout(function() { alert("Time to take the cookies out of the oven"); }, 600000);
如果需要从函数返回一个函数,也可返回一个函数表达式
四、数据结构
数组(Arrays)
创建数组
用数组的字面量(array
literal)创建数组(即详细地指定了该数组包含的元素)
var flavors = ["vanilla", "butterscotch", "lavender", "chocolate", "cookie dough"];
在[]中依次输入每个数据,用,隔开即可。
数组中每一项的数据类型可以是任意的,字符串,布尔值,数值,甚至数组,函数和对象都可以。
数据的个数也没有限制,但受制于计算机内存。
不要求一个数组中所有值的类型都相同。
访问数组元素
var flavorOfTheDay = flavors[2];
[]中的数字表示数组的索引位置,数组的索引从0开始
修改数组元素
flavors[3] = "vanilla chocolate chip";
确定数组的长度
var numFlavors = flavors.length;
每个数组都有一个length属性,它的值也就是数组的长度,等于数组的最大索引值+1
给数组添加新元素
只需给指定索引处的元素赋值即可,但必须小心地指定索引,否则数组将是稀疏的(sparse)
1 2 3 4
var genres = []; genres[0] = "Rockabilly"; genres[1] = "Ambient"; var size = genres.length;
var input = "jenny@wickedlysmart.com"; for(var i = 0; i < input.length; i++) { if (input.charAt(i) === "@") { console.log("There's an @ sign at index " + i); } }
方法indexOf
将一个字符串作为第一个参数,并在字符串中该参数首次出现的位置返回该参数中第一个字符的索引
第二个参数是一个索引,指定从什么位置开始查找
如果没有找到指定的字符串,将返回索引-1
1 2
index = phrase.indexOf("the", 5); console.log("there's a the sitting at index " + index);
方法substring
将两个索引作为参数,提取并返回这两个索引之间的子串
var val = data.substring(5, 10);
返回从索引5到索引10(不包括)的子串
可以省略第二个参数,在这种情况下,substring将提取从指定索引到字符串末尾的子串。
方法split
将一个用作分隔符(delimiter)的字符作为参数,并根据这个分隔符将字符串分成多个部分。
1 2 3 4 5
var data = "name|phone|address"; var vals = data.split("|"); console.log("Split array is ", vals); //Split array is ["name", "phone", "address"]
var scoop = document.getElementById("raspberry"); var altText = scoop.getAttribute("alt"); if (altText == null) { console.log("Oh, I guess there isn't an alt attribute."); } else { console.log("I can't see the image in the console,"); console.log(" but I'm told it looks like " + altText); }