Professional JS(5.1-5.3---Object/Array/Date Type)

一.5-引用类型(Reference Type)
1.本章内容
①Working with objects.
②Creating and manipulating arrays.
③Understanding basic JavaScript data types.
④Working with primitives and primitive wrappers.

2.Object Type
①引用类型是一种数据结构,用于将数据和功能组合在一起。
②两种创建Object实例的方式
a)Object构造函数:

var person=new Object();
person.name='yyc';
person.age=21;

b)Object literal notation(对象字面量表示法)

var person={
name:'yyc',
age:21
};

③引用类型(reference type)又称对象定义(object definitions),因为它们描述的是一类对象所具有的属性和方法。

④当对象字面量定义对象时,实际上不会调用Object构造函数。(Firefox2及更早版本会调用Object构造函数,但Firefox3之后就不会了)

function displayInfo(args){
    var output="";
if(typeof args.name=="string"){
    output +="Name: "+args.name+'\n';
   }
if(typeof args.age=='number'){
    output +='Age: '+args.age+'\n';
}
console.log(output);
}
displayInfo({
    name:'yyc',
    age:21
});

This pattern for argument passing is best used when there is a large number of optional arguments that can be passed into the function.Generally speaking,named arguments are easier to work with but can not get widely when there are numerous optional arguments.The best approach is to use named arguments for those that are required and an object literal(对象字面量) to encompass(封装) multiply optional

arguments.
⑥两种访问对象属性的方式:
a)dot notation: person.name
b)bracket(方括号) notation: person[‘name’]

⑦方括号表示法优点:
a)能直接通过变量访问属性
var propertyName=’name’;
console.log(person[propertyName]);
b)可表示包含会导致语法错误的字符、关键字、保留字的属性名。
person[‘first name’]=’yyc’;
因为属性名可包含非字母非数字。

3.Array类型
①ECMAScript中的数组虽然同其他语言中的数组都是数据的有序列表,但有两个不同之处:
a)数组中的每一项都可以保存任何类型的数据。
b)数组的大小可动态调整。

②两种创建数组的方式:
a)Array构造函数:var colors=(new) Array();
b)Array字面量表示法(Array literal notation):由一对包含数组项的方括号表示,多个数组项以逗号分隔。
var colors=[‘red’,’green’,’blue’];

③与对象一样,使用数组字面量表示法也不会调用Array构造函数(Firefox3及更早版本除外)。

④A unique characteristic of length is that it’s not read-only.By setting the length property,you can easily remove items from or add items to the end of the array.

var colors=['red','green','blue'];
colors.length=2;
colors[2];//undefined;
colors;//["red","green"]

var colors=['red','green'];
colors[colors.length]='blue';
colors;//["red","green","bule"]

因为colors.length实际上是一个数值,而且始终代表该数组最后一项的后一个。
这里写图片描述

这里写图片描述

4.Detecting Arrays(检测数组)
①ECMAScript5新增了Array.isArray()方法
目的:确定某个值是不是数组,无论它创建于哪个全局执行环境。
②全局执行环境是由于网页中的多个框架导致的,如果就一个全局执行环境,用instanceof Operator即可。

5.Conversion Methods转换方法
①所有对象都具有toLocaleString(),toString(),valueOf()方法。

var colors=['red','green','blue'];
colors.toString();//"red,green,blue"
colors.valueOf();//["red","green","blue"]
colors.toLocaleString();//"red,green,blue"


toLocaleString()方法经常会返回与toString()或valueOf()方法相同的值。
这里写图片描述

④The inherited methods toLocaleString(),toString(),valueOf() each return the array items as a comma separated string.It’s possible to construct a string with a different separator using the join() method.The join() method accepts one argument,which is the string separator to use,and returns a string containing all items.
这里写图片描述

⑤If an item in the array is null or undefined,it is represented by an empty string in the result of join(),toLocaleString(),toString() and valueOf().

6.Stack Methods(栈方法)
①栈:限制插入和删除项的数据结构。LIFO:Last In First Out.
②栈中项的插入(push)和移除(pop)只发生在一个位置—栈的顶部。

*③The push() method accepts any number of arguments and adds them to the end of the array,returning the array’s new length.The pop() method,on the other hand,removes the last item in the array,decrements the array’s length and return that item.

7.Queue Methods(队列方法)
①队列数据结构的询问规则:FIFO:First In First Out
②shift()方法取得数组的第一项。

这里写图片描述

③unshift()与shift()方法功能相反,在数组前端添加任意个项并返回新数组长度。
这里写图片描述

④Internet Explorer 7 and ealier always return undefined,instead of the new length of array,for unshift().Internet Explorer 8 returns the length correctly when not in compatibilitu mode.

8.Reordering Methods重排序方法
①已存在数组中的两种重排序方法:reverse(),sort()

var value=[1,2,3,4,1999];
value.sort();//[1,1999,2,3,4]

即使数组中的每一项都是数值,sort()方法比较的仍是字符串【字符编码】

③reverse(),sort()方法的返回值都是经过排序后的数组。

9.Manipulation Methods操作方法
①concat();不能单独改变,需将值存入变量。
这里写图片描述

②slice()方法–
a)一个参数,[a,],返回从该参数指定位置开始到当前数组末尾的所有项。
b)两个参数,[a,b)
这里写图片描述

*③slice()方法不会影响原始数组。

④在一个包含5项的数组中调用slice(-2,-1)于调用slice(3,4)结果相同。(-2+5=3,-1+5=4)—-如果结束位置小于起始位置,返回空数组。

⑤splice()方法—在上一步的基础上改变,而非原始数组。
a)deletion:splice(a,b)—a:起始位置/b:要删除的项数。
b)Insertion:splice(a,b,c)—a:start/b:要删除的项数/c:插入的任意数量的项。
c)Replacement:splice(a,b,c)—同上
这里写图片描述

10.Location Methods位置方法—查找特定项在数组中的位置
①indexOf()正/lastIndexOf()反
这里写图片描述

②包含两个参数:indexOf(a,b)—-a:要查找的项/(b:start起始位置,可省略)

11.Iterative Methods迭代方法
①ECMAScript5 defined five iterative methods for arrays.Each of the methods accepts two arguments: a function to run on each item and an optional scope object in which to run the function(affecting the value of this)
a)every()—-全1为1。
b)some()—-有1位1。
c)filter()—-过滤,筛选出符合条件的项。
d)map()—-保持原数组的完整性,同时进行某些操作。
e)forEach()—-类似于for循环,无返回值。

这里写图片描述

这里写图片描述

这里写图片描述

function(item,index,array)

12.Reduction Methods归并方法
①reduce()—从头至尾/reduceRight()—从尾至头
②均接受两个参数:a:在每一项上都调用的函数/(b:作为归并基础的初始值—可选)
③reduce()&reduceRight()的函数接受4个参数:前一个值,当前值,项的索引,数组对象。
function(prev,cur,index,array)
这里写图片描述
这里写图片描述

13.The Date Type日期对象
①UTC:Coordinated Universal Time国际协调时间
②GMT:GreenWich Mean Time
③PST:Pacific Standard Time
④var now=new Date();
⑤根据特定的日期和事件创建日期对象,有两种方法:
a)Date.parse()—-接受一个表示日期的字符串参数。
b)Date.UTC()—-的参数分别是:年份,基于0的月份(一月是0),月中的哪一天(1-31),小时数(0-23),分钟,秒,以及毫秒。

⑥Date.now()—返回表示调用这个方法时的日期和时间的毫秒数。
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值