Javascript 关于array、object、+= 基本理解
2016-08-09 13:48 yojiaku 阅读(241) 评论(0) 编辑 收藏 举报<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>Example</title> </head> <body> <!-- the best method to quote javascript is putting it before the tag </body> --> <script type="text/javascript"> /* declare an array*/ var beatles = Array("John","Paul","George","Ringo"); /* we also can declare an array like this: var beatles = ["John","Paul","George","Ringo"]; the most important thing is : you can use string to instead label subscript==> var lennon = Array(); lennon["name"] = "John"; lennon["year"] = 1940; lennon["living"] = false; However, in fact, you just add three attributes to array lennon. So, do not use this method. In fact, Array is a native object: var array = []; <=> var array = new Array(); So, if you want to know how long the array is, you can use the function which includes in the Array(): array.length; */ /* build an object */ var lennon = object(); lennon.name = "John"; lennon.year = 1940; lennon.living = false; /* It is noteworthy that Object via "." to obtain attributes. And there is a much easier way to build an Objet: var lennon = {name:"Jhon", year:1940, living:false}; We can also put object into array, and via this way to obtain it: var beatles = Array(); beatles[0] = lennon; Then we can use "beatles[0].name" to obtain the value "Jhon". It is really easier to remember. We can also put one object into another object: var beatles = {}; beatles.vocalist = lennon; Then we can use "beatles.vocalist.name" to obtain the value "John", use "beatles.vocalist.year" to obtain the value "1940" and so on. */ /* A very useful operator : += */ var year = 2010; var message = "This year is "; message += year; alert(message); /* It will ouput : This year is 2010 ; The operator "+=" can mean "addition & assignment" , and it can also mean "concatention & assignment". */ /* global variable and local variable (you can use the keyword "var" to set scope for variables)*/ function square(num) { total = num * num; return total; } var total = 50; var number = square(20); alert(total); /* It will output : 400 ; it means the total is a global variable and the function square() change the value of total and this is not my intention. However, we can use "var" to let the total which in square() become a local variable: function square(num) { var total = num * num; return total; } Then we invoke squre(): var total = 50; var number = squre(20); alert(total); And this will output : 50. We can see it clearly that we protect the global variable "total". */ /* native object */ var number2 = 7.561; var number2 = Math.round(number2); alert(number2); /* It will output: 8. var number2 is an instance of native object "Math", then you can use function round() which is included in "Math" */ var current_date = new Date(); var today = current_date.getDay(); /* The same to "Math", Date is also a native object, it provides getDay(), getHours(), getMonth(). */ </script> </body> </html>
所有的解释都清楚地写在代码中了,o(* ̄▽ ̄*)o!