Enhanced typeof function
o.toString() // May invoke a customized toString( ) method for the object
Instead, we have to refer explicitly to the default toString( ) function as the Object.prototype.toString object and use the apply( ) method of the function to invoke it on the desired object:
Object.prototype.toString.apply(o); // Always invokes the default toString( )
We can use this technique to define a function that provides enhanced "type of" functionality:
// An enhanced "type of" function. Returns a string that describes the
// type of x. Note that it returns "Object" for any user-defined object types.
function Typeof(x) {
// Start with the typeof operator
var t = typeof x;
// If the result is not vague, return it
if (t != "object") return t;
// Otherwise, x is an object. Get its class value to try to
// find out what kind of object it is.
var c = Object.prototype.toString.apply(x); // Returns "[object class]"
c = c.substring(8, c.length-1); // Strip off "[object" and "]"
return c;
}
Value of function
The valueOf( ) method is much like the toString( ) method, but it is called when JavaScript needs to convert an object to some primitive type other than a string -- typically, a number. Where possible, the function should return a primitive value that somehow represents the value of the object referred to by the this keyword.
function Complex(real, imaginary) {
this.x = real; // The real part of the number
this.y = imaginary; // The imaginary part of the number
}
// Return the real portion of a complex number. This function
// is invoked when a Complex object is treated as a primitive value.
Complex.prototype.valueOf = function( ) { return this.x; }
var a = new Complex(5,4); // the value of a is 5
var b = new Complex(2,1); // the value of b is 2
One note of caution about defining a valueOf( ) method:
the valueOf( ) method can, in some circumstances, take priority over the toString( ) method when converting an object to a string. Thus, when you define a valueOf( ) method for a class, you may need to be more explicit about calling the toString( ) method when you want to force an object of that class to be converted to a string. To continue with the Complex example:
alert("c = " + c); // Uses valueOf( ); displays "c = 3"
alert("c = " + c.toString( )); // Displays "c = {3,3}"
Array Index
Note that array indexes must be integers greater than or equal to 0 and less than 232 -1. If you use a number that is too large, a negative number, or a floating-point number (or a boolean, an object, or other value), JavaScript converts it to a string and uses the resulting string as the name of an object property, not as an array index.
Thus, the following line creates a new property named "-1.23"; it does not define a new array element:
a[-1.23] = true;
Contiguous Array
var fruits = ["mango", "banana", "cherry", "pear"];
for(var i = 0; i < fruits.length; i++)
alert(fruits[i]);
This example assumes, of course, that elements of the array are contiguous and begin at element 0. If this were not the case, we would want to test that each array element was defined before using it:
for(var i = 0; i < fruits.length; i++)
if (fruits[i] != undefined) alert(fruits[i]);
Converting Numbers to Strings
The toString( ) method of the Number object (primitive numbers are converted to Number objects so that this method can be called) takes an optional argument that specifies a radix, or base, for the conversion. If you do not specify the argument, the conversion is done in base 10. But you can also convert numbers in other bases (between 2 and 36).[3] For example:
var n = 17;
binary_string = n.toString(2); // Evaluates to "10001"
octal_string = "0" + n.toString(8); // Evaluates to "021"
hex_string = "0x" + n.toString(16); // Evaluates to "0x11"
分享到:
相关推荐
Part II: JavaScript Tutorial - Summary. Chapter 4: Browser and Document Objects. Chapter 5: Scripts and HTML Documents. Chapter 6: Programming Fundamentals, Part I. Chapter 7: Programming ...
**前端开源库-karma-verbose-summary-reporter** `karma-verbose-summary-reporter` 是一个专为前端开发者设计的开源库,它与 Karma 集成,提供了更详尽的测试结果摘要报告。Karma 是一个流行的自动化测试运行器,...
将GITHUB_TOKEN替换为您的回购密钥并触发操作后,您可以使用profile-summary-card-output文件夹中的所有内容。添加到现有存储库添加此操作以回购,并将yml文件中的GITHUB_TOKEN替换为您的回购密码。GitHub动作用法...
JavaScript是一种广泛应用于Web开发的脚本语言,尤其在前端领域占据主导地位。"JavaScript高级"这一主题涵盖了语言的深入理解和高级用法,包括但不限于原型、闭包、异步编程、模块化、性能优化以及ES6及以后的新特性...
- **JS Summary**:总结JavaScript的核心概念和技术要点。 #### JS示例 - **JS Examples**:提供超过200个示例代码,涵盖从简单到复杂的各种应用场景。 - **JS Objects Examples**:面向对象编程的示例,展示如何...
【标题】"lectures_summary-源码.rar" 提供的是一种学习资源的压缩包,很可能包含一系列编程课程的源代码。从"source code"这个词我们可以推测,这份资料可能与编程教学或者软件开发有关,可能是为了帮助学生或...
通常,"summary-20230720.zip"这样的文件名可能表示这是一个包含2023年7月20日总结或报告的压缩文件。如果这是一份关于IT行业的总结,可能涵盖了技术趋势、最新产品发布、行业新闻、代码示例或项目更新等内容。 ...
gitbook-plugin-summary Gitbook插件自动生成SUMMARY.md介绍这个插件是出于无法从基本树结构自动生成SUMMARY.md文件的挫败感而创建的。 结果是你安装了这个插件,它就在你当前的book.json文件book.json 。 无需...
总结---An-Chrome-Extension 当鼠标悬停在网站链接上时显示摘要的扩展要使用此扩展程序,您需要有 chrome。 与此扩展一起安装后,当您将鼠标移到链接上时,将显示一个附加窗口。 该窗口将显示链接的基本信息,即最大...
npm i @codersrank/summary --save 安装后,您需要导入和注册Web组件: import CodersrankSummary from '@codersrank/summary' ; // register web component as <codersrank> element window . customElements . ...
配置除了Jest提供的默认报告程序之外,还添加jest-summary-reporter : { "reporters": [ "default", "jest-summary-reporter" ]}如果您还想查看通过/未决的测试,请执行以下操作: { "reporters": [ "default", [...
【标题】"summary-news-app:AI汇总的新闻应用程序"是一个基于现代Web技术和人工智能技术构建的项目,旨在为用户提供高效、个性化的新闻阅读体验。通过利用先进的自然语言处理(NLP)算法,特别是BERT模型,该应用...
JavaScript摘要 <개요> JavaScript연산자연산자연산자메소드중중중토토토토토토토토토향향향그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을원함원함원함 ...
标题中的“ant - build.xml - summary”指的是Apache Ant工具中的构建文件`build.xml`的概要。Apache Ant是一个广泛使用的Java项目自动化构建工具,类似于Unix的make,但基于XML。在Ant中,`build.xml`是项目的配置...
Github配置文件摘要卡繁体中文此存储库受profile-summary-for-github的启发,此操作将生成github配置文件摘要卡并推送到您的存储库。 添加此动作后,您也可以自己触发动作。 将其添加到工作流程后,应触发工作流程,...
ISBN-13: 9781617291951 Summary JavaScript Application Design: A Build First Approach introduces JavaScript developers to techniques that will improve the quality of their software as well as their ...
> npm install gulp-file-summary --save-dev用var fileSummary = require ( 'gulp-file-summary' ) ;gulp . task ( 'summary' , function ( ) { return gulp . src ( './lib/*.js' ) . pipe ( fileSummary ( { ...
`python-profile-summary-for-github` 是一个专为GitHub用户设计的工具,它提供了可视化的配置文件摘要,帮助用户更直观地理解并管理他们的GitHub个人资料。这个工具基于Python编程语言,利用了Flask框架来构建web...
npm install summary-statistics --save 用法 var ss = require ( 'summary-statistics' ) var values = [ 3 , 14 , 15 , 92 , 65 , 35 , 89 , 79 , 32 , 38 , 46 , 26 , 43 , 38 , 32 , 79 , 5 ] var summary = ...
Serratus-Summary-api-athena 带有示例的端点 /family/ /family/Coronaviridae /family/Coronaviridae?identityMin=80&identityMax=90 /sequence/ /sequence/AY874537_3000883 /sequence/AY874537_3000883?...