ホームページ > ウェブフロントエンド > jsチュートリアル > JavaScript学習ノートを整理するためのリファレンスタイプ_JavaScriptスキル

JavaScript学習ノートを整理するためのリファレンスタイプ_JavaScriptスキル

WBOY
リリース: 2016-05-16 15:18:49
オリジナル
1104 人が閲覧しました

Reference types are very important in JavaScript. A reference type is a data structure used to organize data and functionality together. It describes the properties and methods of a type of object. Object is a basic type, Array is an array type, Date is a date type, RegExp is a regular expression type, etc.

Embrace JavaScript

The once unknown JavaScript has doubled in value with the popularity of AJAX. Now JavaScript is no longer just a dispensable auxiliary tool in WEB development. There is even a position dedicated to it "JavaScript Engineer". Then Even if you are just a WEB backend development programmer, you must know JavaScript. At least in some relevant job requirements, you can see the words "familiarity with JavaScript is preferred". I even want to tell you that you will be able to develop desktop software with JavaScript, thanks to another development model of Adobe AIR, which is to develop AIR with HTML+CSS+JavaScript.

1.Object type

1. Create:

var dog = new Object(); 
ログイン後にコピー

Commonly used to store and transmit data. For example, storage:

var person = new Object();
person.name = "Nicholas";
person.age = 29; 
ログイン後にコピー

The second way to create: (When creating, the attribute name can also be in string format, that is, you can add quotes to the attribute name.)

var person = {
name : "Nicholas",
age : 29
}; 
ログイン後にコピー

2. Get the attribute value: person["name"]; or: person.name;

2.Array type

The same array can store any type of data (a hodgepodge).

1. The array can be dynamically adjusted (add one more data, it will grow in length by itself, it is not dead.).

2. Create:

var stars=new Array();//方式1
var stars=new Array(20);//方式2
var stars=new Array("周杰伦","林俊杰","孙燕姿");//方式3
var stars=Array(20);//方式4
var stars=["周杰伦","孙燕姿","林俊杰"];//方式6 
ログイン後にコピー

3. Dynamic adjustment example:

var stars=["周杰伦","林俊杰","孙燕姿"];
stars[1]="JJ";//动态改变(把林俊杰变为JJ)
stars[3]="皮裤汪";//动态增长(加了一个长度)
stars.length=1;//动态强制缩减(林俊杰、孙燕姿、皮裤汪强制移除,长度变为1) 
ログイン後にコピー

4. Detect array: Array.isArray(value);

5. Use join() to convert the array into a delimited string:

var stars = ["周杰伦", "王尼玛", "张全蛋"];
alert(stars .join(",")); //周杰伦,王尼玛,张全蛋
alert(stars .join("-")); //周杰伦-王尼玛-张全蛋 
ログイン後にコピー

6. You can use arrays like stacks (pop() out, push() in).

7. Arrays can be used like queues. (Combining shift() and push()):

var stars = new Array(); //create an array
var count = colors.push("周杰伦", "王尼玛"); //push two items
alert(count); //2
count = stars .push("张全蛋"); //push another item on
alert(count); //3
var item = colors.shift(); //get the first item
alert(item); //周杰伦
alert(colors.length); //2
/**所谓栈变队列,其实就是把栈颠倒过来再拉取*/ 
ログイン後にコピー

8. Sort.

1.reverse()Reverse the order of the array; (return the sorted array)

2.sort()Sort from small to large. But it is sorted by strings, not by numbers: (returns the sorted array).

var values = [, , , , ];
values.sort();
alert(values); //,,,,
ログイン後にコピー

To sort the way you expect, you can add a comparison function as a parameter to sort():

function compare(value, value) {
if (value < value) {
return -;
} else if (value > value) {
return ;
} else {
return ;
}
}
var values = [, , , , ];
values.sort(compare);
alert(values); //,,,, 
ログイン後にコピー

Simplified version of the comparison function (sort only cares whether it returns a positive number, a negative number or 0):

function compare(value1,value2){
return value2 - value1; 
} 
ログイン後にコピー

9. Operations on arrays: joining, slicing, splicing.

1. Connection : use concat, memory: concat-->concatenate: connection, chain.

Example:

var stars = ["周杰伦", "王尼玛", "张全蛋"];
var stars = stars .concat("太子妃", ["花千骨", "梅长苏"]);
alert(stars); //周杰伦,王尼玛,张全蛋 
alert(stars); //周杰伦,王尼玛,张全蛋,太子妃,花千骨,梅长苏 
ログイン後にコピー

2. Slice . Use slice, memory: slice translation: slice. Example:

var stars = ["梅长苏", "誉王", "靖王", "霓凰", "飞流"];
var stars= stars.slice();
var stars= stars.slice(,);
alert(stars); //誉王,靖王,霓凰,飞流(从第一个位置开始切)
alert(stars); //誉王,靖王,霓凰(从第个位置切到第个位置,表示半封闭,不包含) 
ログイン後にコピー

3. Splicing. splice. Powerful. Can be deleted, inserted, replaced.

1.Delete any number of items: For example: splice(0,2), delete the 0th and 1st items (semi-closed interval) (return the deleted items).

2.Insert any number of items at the specified position: for example: splice(2,0,"Jay Chou","王尼马"), insert Jay Chou and Wang NIMA starting from the 2nd position Two items.

3.Insert any number of items at the specified position and delete any number of items at the same time. For example: splice(2,1,"Jay Chou","Wang Nima"), delete one item from the second position, and then start inserting two items, Jay Chou and Wang Nima.

10. Position method: indexOf, lastIndexOf;

11. Iterative method: Divided into: passing only if all are qualified, passing if any one is qualified, filtering some residue, one-to-one mapping, iterative query, and reduction.

1. Pass only if all are qualified:

var numbers = [1,2,3,4,5,4,3,2,1];
var everyResult = numbers.every(function(item, index, array){
return (item > 2);
});
alert(everyResult); //false 
ログイン後にコピー

In the above example, true will be returned only if each item is greater than 2.

2. Pass if you pass any one:

var numbers = [1,2,3,4,5,4,3,2,1];
var someResult = numbers.some(function(item, index, array){
return (item > 2);
});
alert(someResult); //true 
ログイン後にコピー

In the above example, if one is greater than 2, it will return true.

3. Filter part of the residue:

var numbers = [1,2,3,4,5,4,3,2,1];
var filterResult = numbers.filter(function(item, index, array){
return (item > 2);
});
alert(filterResult); //[3,4,5,4,3] 
ログイン後にコピー

In the above example, all values ​​greater than 2 are filtered out.

4. One-to-one mapping:

var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
return item * 2;
});
alert(mapResult); //[2,4,6,8,10,8,6,4,2] 
ログイン後にコピー

In the above example, multiply each term by 2.

5. Iteration: use for-each.

6. Reduce: reduce.

var values = [1,2,3,4,5];
var sum = values.reduce(function(prev, cur, index, array){
return prev + cur;
});
alert(sum);//15
ログイン後にコピー

累加求和返回,5项缩为1项。 

3.RegExp类型

1.var expression=/ pattern / flags;

2.flags分三种:g(global全局模式,应用于所有字符串)、i(case-insensive,忽略字母大小写)、m(multiline,多行模式,一行检验完了接着下一行。)。举例:

/**匹配字符串中所有'at'的实例*/
var pattern1=/at/g;
/**匹配第一个'bat'或'cat',不分大小写*/
var pattern2 =/[bc]at/i;
/**匹配所有以'at'结尾的3个字符组合,不分大小写*/
var pattern3=/.at/gi; 
ログイン後にコピー

3.模式中所有的元字符必须转义,元字符:( { [ \ ^ $ | ) ? * + . ] }

4.Function类型

1.每个函数都是Function类型的实例,而且与其他引用类型一样,都有属性和方法。

2.函数的两种定义方法:

方法1:

function sum(a,b){
return a + b; 
} 
ログイン後にコピー

方法2:

var sum=function(a,b){
return a + b;
} 
ログイン後にコピー

3.函数没有重载。

5.Boolean、Number、String:基本包装类型

var a="Jay Chou is a superstar";
var b=a.substring(0,8); 
ログイン後にコピー

上例中,a是基本类型,但是a可以调用substring方法,是因为,后台自动完成a的包装操作,创建String类型的一个实例。Boolean,Number也类似。

6.单体内置对象,不需要实例化,直接使用,如:Math,Global。

1.所有全局作用域中定义的函数、变量,都是Global对象的方法,比如:parseInt,isNaN等。

2.eval()方法也是Global对象的方法,它负责解析javascript。

3.Math对象是保存数学公式和相关信息的。它有很多方法, 如:min求最小值,max求最大值,ceil()向上取整,floor向下取整,round四舍五入,random取随机数。

ps:引用类型理解:变量的交换等于把现有一间店的钥匙(变量引用地址)复制一把给了另外一个老板,此时两个老板同时管理一间店,两个老板的行为都有可能对一间店的运营造成影响。

引用类型例子

function chainStore()
{
var store1=['Nike China'];
var store2=store1;
alert(store2[0]); //Nike China
store1[0]='Nike U.S.A.';
alert(store2[0]); //Nike U.S.A.
}
chainStore();
//在上面的代码中,store2只进行了一次赋值,理论上它的值已定,但后面通过改写store1的值,发现store2的值也发生了改变,这正是引用类型的特征,也是我们要注意的地方
ログイン後にコピー
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート