JavaScript Review (3)

Dictionary (Array objects)

  1. When using the Array object dictionary, .length property can not be used

  2. If you want to access the object element, you can not use an index, use only key

  3. If you traverse the dictionary can only be used for ...... in statement

Another variable is the dictionary container model, and can store any type of object
  1. Each dictionary key key => value of the colon: segmentation, key-value pair with a comma between each split, including the entire dictionary in braces {} in

  2. Generally unique key, if the last repetition of a key-value replaces the preceding value need not be unique

  3. Value may take any data type, but the key must be immutable, such as a string, a tuple

  • Creating dictionaries and general traversal
var arr = new Array();
arr["001"] = "牛一";
arr["002"] = "王二";
arr["003"] = "张三";
arr["004"] = "李四";
for(var key in arr){
    console.log(key + ":" + arr[key]);
}
  • Quickly create and iterate dictionary
var arr = {"name":"牛一","number":"123456","age":"20","address":"16号楼520"};
//键值对:key:value
for(var key in arr){
    console.log(key + ":" + arr[key]);
}
//console.log(arr.key);通过属性来访问对应的值

Json objects

1.Json target tissue is a data format, key-way is in the form of a set of stored data

2.JSON data format of key / value pairs, like JavaScript object attribute key / value pairs include field names (in double quotes), followed by a colon, followed by the value

3.JSON value can be:

  • Number (integer or floating point)

  • String (in double quotes

  • A logical value (true or false)

  • Array [in brackets]

  • Objects in curly braces {}

  • null

4.JSON using JavaScript syntax

//遍历Json对象
var stuArr = [
    {"name":"张三","address":"郑州人","age":"20"},
    {"name":"李四","address":"上海人","age":"21"},
    {"name":"王五","address":"北京人","age":"19"},
    {"name":"赵六","address":"四川人","age":"20"}
];
for (let i = 0; i < stuArr.length; i++) {
    console.log("name:" + stuArr[i].name);
    console.log("address:" + stuArr[i].address);
    console.log("age:" + stuArr[i].age);
}

JavaScript function expansion

1. Use the prototype (prototype object) add new properties or methods for an object that already exists.

//输出表情符号
String.prototype.mood = function(mind){
    if(!mind){
        mind = "^";
    }
    return mind + "_" + mind;
}
var name = "_";
console.log(name.mood("♡"));

2. Use the prototype (prototype object) add new properties or methods custom objects.

//输出一个人的全名
function Person(surname,name){
    this.surname = surname;
    this.name = name;
}

Person.prototype.Fullname = function(){
    return this.surname + this.name;
};
var people = new Person("西门","吹雪");
console.log(people.Fullname());

Write function normally will expand into an external file, import use.

Global variables and local variables

Local variables

Definition: variables declared within a function

Scope: local scope.

Life cycle: the local variable is destroyed after the function is finished.

Global Variables

Definition: No variable declarations (var keyword is not used) in the function or variable is a function defined outside, that is a global variable.

Scope: Global variables have global scope, scripts and web pages all functions can be used.

Life cycle: Global Variables destroyed after the page is closed.

Guess you like

Origin www.cnblogs.com/discourage/p/11530571.html