`

7 Object And Array

 
阅读更多

1. 首先就是Object可以用作Hash, Map,关联数组

 

2. Object的属性和方法

hasOwnProperty()

propertyIsEnumerable()

isPrototypeOf()

 

3. Array的属性和方法

首先要注意Array[index]中的index不会做任何trunction这种操作,什么意思呢,看下面的代码

var arr = [0,1,2,3];    
//!arr["1.23"] = 1.23;
document.write("arr[1] is " + arr[1] + "<br>");
document.write("arr[1.23] is " + arr[1.23] + "<br>");     
arr[10] = 10;
document.write("arr is " + arr + "<br>");      

输出的结果是:

arr[1] is 1
arr[1.23] is undefined
arr is 0,1,2,3,,,,,,,10

 

其次,要注意Array的length属性是可写的。也就是你可以改变它。看下面的代码:

var arr = [1,2,3];
undefined
arr;
[1, 2, 3]
arr.length = 5;
5
arr;
[1, 2, 3, undefined, undefined]
arr.length = 1;
1
arr;
[1]
 

Array的方法

 

1.  join() 可以带一个分隔符,默认是","。不会改变数组

 

2.  reverse() 反转数组,直接改变数组

 

3.  sort() 排序,默认以字典顺序排序,改变数组

var arr2 = [-1, -100, 2, 35];
undefined
arr2.sort();
[-1, -100, 2, 35]
arr2;
[-1, -100, 2, 35]
arr2.sort(function(a,b){return a - b;});
[-100, -1, 2, 35]
arr2
[-100, -1, 2, 35]

 这个对于数字来说非常的不方便:(

 

4.  concat 连接操作。不改变数组,返回拼接好的数组

var arr = [1,2,3,4];
undefined
arr.concat(4,[5,[6,7]]);
[1, 2, 3, 4, 4, 5, [6, 7]]
arr;
[1, 2, 3, 4]

 第一层的子数组会被展开,但是再深度的就不会了。这点让人觉得蛮困惑的,为什么这么设计呢?

 

5.  slice(a, b) 返回[a, b)的部分,不改变数组

var arr = [1,2,3,4,5];
undefined
arr.slice(-3,-1);
[3, 4]
arr;
[1, 2, 3, 4, 5]

 

6. splice(a, length, addOne, addTwo ...)

这个函数,首先删除[a, a+1, a+length-1],然后再加上addOne, addTwo。改变数组

var a = [1,2,3,4,5];
a.splice(2,0,'a','b');  // Returns []; a is [1,2,'a','b',3,4,5]
a.splice(2,2,[1,2],3);  // Returns ['a','b']; a is [1,2,[1,2],3,3,4,5]

 

7. push, pop, unshift, shift 改变数组

这些函数都蛮正常的理解,首先它会返回值,无论是push, unshift还是pop和shift,都会。push,unshift是返回数组的长度,pop和shift是返回那个被pop或者shift的值。还需要注意,如果一次unshift很多值,值是一次进去的,所以顺序和你在参数列表里一致(不需要理解这句话,看下面的例子就知道)

var arr = [1,2,3,4,5];
undefined
arr.push(6);
6
arr.pop();
6
arr.push(6,7,8);
8
arr.pop();
8
arr;
[1, 2, 3, 4, 5, 6, 7]
arr.unshift(0);
8
arr;
[0, 1, 2, 3, 4, 5, 6, 7]
arr.shift();
0
arr;
[1, 2, 3, 4, 5, 6, 7]
arr.unshift(-3,-2,-1,0);
11
arr;
[-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
arr.shift();
-3
arr;
[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
 

8 toString(), 不会改变数组。注意下面的例子:

arr;
[1, 2, 3, [4, 5, [6, 7]]]
arr.toString();
"1,2,3,4,5,6,7"
arr.join();
"1,2,3,4,5,6,7"

 数组全部展开了。

分享到:
评论

相关推荐

    superobject1.5.rar

    CHANGES: * V1.5 * + 修正indent[aaa,bbb] 的问题 ,后面没换行的问题,主要是美化。 ... * V1.4 * + 修正D7下关闭溢出出错的问题{.$.Q-} ... * + Null Object Design Patern (ex: for obj in values.N['path'] do ...)

    Java, Java, Java, Object-Oriented Problem Solving (3rd Edition 2016)

    7. Strings and String Processing. 8. Arrays and Array Processing. 9. Graphical User Interfaces. 10. Graphics and Drawing. 11.Exceptions: When Things Go Wrong. 12. Recursive Problem Solving. 13. ...

    Professional JavaScript for Web Developers英文版

    language concepts including syntax and flow control statementsvariable handling given their loosely typed naturebuilt-in reference types such as object and arrayobject-oriented programingpowerful ...

    PHP.Arrays.in.PHP.7

    After a quick overview of PHP 7, each chapter concentrates on single, multi-dimensional, associative, and object arrays. PHP Arrays is a first of its kind book using PHP 7 that demonstrates inserting...

    Python 3 Object-Oriented Programming(PACKT,2ed,2015)

    Starting with a detailed analysis of object-oriented analysis and design, you will use the Python programming language to clearly grasp key concepts from the object-oriented paradigm. This book fully ...

    Vcl.SuperObject_superobject_class_

    `SuperObject` 类提供了一个对象化的接口,可以方便地创建和操作 JSON 对象,包括对象(JSON Object)、数组(JSON Array)、字符串(JSON String)、数字、布尔值和 null。 描述中提到的 "+ JavaToDelphiDateTime ...

    MySQL and JSON A Practical Programming Guide 2018

    7 Generated Columns Using Generated Columns Columns Generated from JSON Generated Columns: Common Errors 8 GeoJSON ST_GeomFromGeoJSON ST_AsGeoJSON 9 PHP’s JSON Functions JSON_DECODE JSON_ENCODE 10 ...

    AFNetworking第三方网络数据请求类-iOS开发必备

    7. **NSURLSession的封装**:AFNetworking基于Apple的NSURLSession API进行封装,这使得它能充分利用iOS系统的网络栈,包括多任务并发、断点续传、后台下载等特性。 8. **安全性**:AFNetworking支持HTTPS请求,...

    PHP Objects Patterns and Practice(5th)

    - SPL提供了一系列内置的接口、类和迭代器,如ArrayObject、SplStack、SplQueue等,帮助开发者处理数组、集合和其他数据结构。 7. PHP命名空间(Namespaces): - 解决类名冲突问题,使代码组织更加清晰。 - ...

    bsearch.zip

    to an array member, in that order, and should return an integer less than, equal to, or greater than zero if the key object is found, respectively, to be less than, to match, or be greater than the ...

    Object-Oriented JavaScript, 3rd Edition-Packt Publishing(2017).epub

    Modern JavaScript embraces a vast array of time-tested and cutting edge features. Several of these features are slowly giving shape to the next generation of web and server platforms. ES6 introduces ...

    Scala and Spark for Big Data Analytics

    Work on a wide array of applications, from simple batch jobs to stream processing and machine learning Explore the most common as well as some complex use-cases to perform large-scale data analysis ...

    Oracle Database 12c PL-SQL programming

    Work with collections, varrays, tables, and associative array collections Locate and repair errors and employ exception handlers Execute black box, white box, and integration tests Configure and ...

    Ember.js.Web.Development.with.Ember.

    you will explore its object-oriented pattern, cover classes and other properties, diving into great techniques to define your routes when managing applications, and using object and array controllers...

    Apress.PHP.Arrays.Single.Multi-dimensional.Associative.and.Object.Arrays.

    Apress.PHP.Arrays.Single.Multi-dimensional.Associative.and.Object.Arrays.in.PHP.7.1484225554.rar 最新书籍,精讲PHP数组,文字版PDF

    EXECUTABLE AND LINKABLE FORMAT

    **ELF**, or **Executable and Linkable Format**, is a standard file format used for executables, object code, shared libraries, and core dumps. It was originally developed by UNIX System Laboratories ...

Global site tag (gtag.js) - Google Analytics