`

Javascript Array (from javascript:the good parts)

阅读更多

Confusion

A common error in JavaScript programs is to use an object when an array is required

or an array when an object is required. The rule is simple: when the property names

are small sequential integers, you should use an array. Otherwise, use an object.

 

how to distinguish between arrays and objects?

 JavaScript does not have a good mechanism for distinguishing between arrays and

objects. We can work around that deficiency by defining our own is_array function:

is_array=function(value){
    return value && (typeof value)==="object" && value.constructor=Array
}

 

 Unfortunately, it fails to identify arrays that were constructed in a different window

or frame. If we want to accurately detect those foreign arrays, we have to work a little

harder:

is_array=function(value){
    return Object.prototype.toString.apply(value)==="[Object Array]";
}

 

Because an array is also an object,so we can add methods directory to an individual array:

var data=[1,2,3];
data.total=function(){
    var result=0;
    for(var i=0;i<this.length;i++){
         result+=this[i];
   }
   return result;
}
data.total();//return 6;

 Since the string "total" is not an integer,so adding a total property to an array does not change its length.

 

分享到:
评论

相关推荐

    JavaScript语言精粹(JavaScript.The.Good.Parts).pdf

    《JavaScript语言精粹》(JavaScript.The.Good.Parts)是由Douglas Crockford所著的一本关于JavaScript编程语言的专业书籍。本书主要目的是帮助读者深入理解并掌握JavaScript的核心概念和技术,通过去除那些容易引起...

    javascript-weird-parts:了解JavaScript的怪异部分

    - **数组与类数组对象**:JavaScript数组有许多内置方法,类数组对象可以通过Array.prototype.slice.call()转换为真正的数组。 - **对象属性的访问方式**:点号和方括号都可以用来访问对象属性,方括号允许使用...

    A samart way to learn javascript

    30. Extracting parts of the date and time(提取日期和时间的部分):从日期对象中提取特定部分,如年、月、日、小时等。 31. Specifying a date and time(指定日期和时间):如何创建一个指定的日期和时间对象。...

    Simplifying [removed] Writing Modern JavaScript with ES5, ES6, and Beyond pdf

    Starting from variable declarations that communicate intention clearly, see how modern principles can improve all parts of code. Incorporate ideas with curried functions, array methods, classes, and ...

    A Smarter Way to Learn JavaScript.pdf

    日期和时间的操作在JavaScript中也有其特有的方法,本书中讲解了获取当前日期和时间(Getting the current date and time)、提取日期和时间的各个部分(Extracting parts of the date and time)、指定日期和时间...

    javascript语言精粹(中英文版)

    Good Parts Section 1.1. Why JavaScript? Section 1.2. Analyzing JavaScript Section 1.3. A Simple Testing Ground Chapter 2. Grammar Section 2.1. Whitespace Section 2.2. Names Section 2.3. ...

    Developer Notes for prototype.doc

    - _JavaScript: The Good Parts_ by Douglas Crockford - _Eloquent JavaScript_ by Marijn Haverbeke #### 参考资料 ##### 扩展 JavaScript 类 Prototype 扩展了多个核心 JavaScript 类,包括 Object、Number、...

    javascript中不易分清的slice,splice和split三个函数_.docx

    在JavaScript中,数组和字符串操作中常常会遇到一些相似但功能各异的方法,其中`slice`、`splice`和`split`就是三个容易混淆的函数。接下来我们将深入理解它们的用法和区别。 **1. `slice()`** `slice()`方法主要...

    Knockout API 中文版

    jQuery doesn’t have a concept of an underlying data model, so to get the number of items you have to infer it from the number of TRs in a table or the number of DIVs with a certain CSS class....

    JS十六进制分割转换.rar

    let hexValue = Array.from(ab).map(b =&gt; b.toString(16)).join(''); // "1a" ``` 5. **十六进制字符串的操作** 如果需要对整个十六进制字符串进行操作,如查找子串、替换子串等,可以使用JavaScript的字符串...

    JavaScript 动态将数字金额转化为中文大写金额

    // Process the conversion from currency digits to characters: // Separate integral and decimal parts before processing conversion: parts = currencyDigits.split("."); if (parts.length &gt; 1) { ...

    python3.6.5参考手册 chm

    The json module: JavaScript Object Notation The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific ...

    js string utils

    在JavaScript中,字符串是基本的数据类型之一,而`js string utils`通常指的是一个包含各种字符串操作函数的工具库。这个库可能包含对字符串进行格式化、处理、验证等任务的实用方法,使得开发者在处理字符串时更加...

    HTML5 图片上传 带预览功能 (pc,手机都能适配)

    var uInt8Array = new Uint8Array(raw.length); for (var i = 0; i ; ++i) { uInt8Array[i] = raw.charCodeAt(i); } return new Blob([uInt8Array], {type: contentType}); } var blob = base64ToBlob...

    html+jQuery+canvas实现手机拍照上传(这里上传的是预览图的大小,原图大小路径获取已上传参考)

    var u8arr = new Uint8Array(raw.length); for (var i = 0; i ; ++i) { u8arr[i] = raw.charCodeAt(i); } return new Blob([u8arr], {type: contentType}); } ``` 总结来说,通过HTML5的`&lt;input type="file"&gt;`...

    JavaScript-3

    Composite Type: 2 parts of Composite Type. ============================================== i) Array: Its start Third Brackets -- [ ] Define: (var var_name= elements) EX: var fruits= ['apple'...

Global site tag (gtag.js) - Google Analytics