`

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)

    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. ...

    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...

    Professional JavaScript for Web Developers, 3rd Edition

    built-in reference types such as object and array object-oriented programing powerful aspects of function expressions Browser Object Model allowing interaction with the browser itself detecting the ...

    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 ...

    Ember.js Web Development with Ember CLI(PACKT,2015)

    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...

    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 ...

    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 ...

    Numerical Methods, Algorithms and Tools in C#

    The vast array of practical examples presented can be easily customized and implemented to solve complex engineering and scientific problems typically found in real-world computer applications.

    Learning.JavaScript.Add.Sparkle.and.Life.to.Your.Web.Pages.3rd.Edition.14

    Arrays and Array Processing Chapter 9. Objects and Object-Oriented Programming Chapter 10. Maps and Sets Chapter 11. Exceptions and Error Handling Chapter 12. Iterators and Generators Chapter 13. ...

    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

    Core.Java.B013WZRDNQ

    Chapter 7 Array and String Chapter 8 Inheritance Chapter 9 Packagee Chapter 10 Multithreading Chapter 11 Exception Handling Chapter 12 Applet Chapter 13 Managing Input/Output Files in Java

    PHP and MYSQL Bilbe [英文原版]

    Chapter 9: Arrays and Array Functions 157 Chapter 10: Numbers 191 Chapter 11: Basic PHP Gotchas 209 Part II: PHP and MySQL 231 Chapter 12: Choosing a Database for PHP 233 Chapter 13: SQL Tutorial ...

    fast-keys:Object.keys()的高性能替代品

    其API具有与Array.prototype相同的许多方法,以及一些其他帮助器。 :fast-forward_button: :racing_car: 真的非常快 :brain: 比Object.keys更少的堆分配和垃圾回收 :hammer_and_wrench: 额外的实用方法 我为什么...

Global site tag (gtag.js) - Google Analytics