`
tory320
  • 浏览: 34022 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

javascript summary11-12(2)

阅读更多
Enumerating Properties
The for/in loop discussed in Chapter 6 provides a way to loop through, or enumerate, the properties of an object. This can be useful when debugging scripts or when working with objects that may have arbitrary properties whose names you do not know in advance. The following code shows a function you can use to list the property names of an object:

function DisplayPropertyNames(obj) {

    var names = "";

    for(var name in obj) names += name + "\n";

    alert(names);

} 

Note that the for/in loop does not enumerate properties in any specific order, and although it enumerates all user-defined properties, it does not enumerate certain predefined properties or methods.

Undefined Properties
If you attempt to read the value of a property that does not exist (in other words, a property that has never had a value assigned to it), you end up retrieving the undefined value (introduced in Chapter 3).

You can use the delete operator to delete a property of an object:

delete book.chapter2;
Note that deleting a property does not merely set the property to undefined; it actually removes the property from the object. The for/in loop demonstrates this difference: it enumerates properties that have been set to the undefined value, but it does not enumerate deleted properties.

Instance Methods and Class Methods
/*

 * Complex.js:

 * This file defines a Complex class to represent complex numbers.

 * Recall that a complex number is the sum of a real number and an

 * imaginary number and that the imaginary number i is the

 * square root of -1.

 */



/*

 * The first step in defining a class is defining the constructor

 * function of the class. This constructor should initialize any

 * instance properties of the object. These are the essential

 * "state variables" that make each instance of the class different.

 */

function Complex(real, imaginary) {

    this.x = real;       // The real part of the number

    this.y = imaginary;  // The imaginary part of the number

}



/*
 * The second step in defining a class is defining its instance
 * methods (and possibly other properties) in the prototype object
 * of the constructor. Any properties defined in this object will
 * be inherited by all instances of the class. Note that instance
 * methods operate implicitly on the this keyword. For many methods,
 * no other arguments are needed.
 */


Complex.prototype.magnitude = function(  ) {

    return Math.sqrt(this.x*this.x + this.y*this.y);

};


/*
 * The third step in defining a class is to define class methods,
 * constants, and any needed class properties as properties of the
 * constructor function itself (instead of as properties of the
 * prototype object of the constructor). Note that class methods
 * do not use the this keyword: they operate only on their arguments.
 */



// Add two complex numbers and return the result.

Complex.add = function (a, b) {

    return new Complex(a.x + b.x, a.y + b.y);

};



// Here are some useful predefined complex numbers.

// They are defined as class properties, where they can be used as

// "constants." (Note, though, that they are not actually read-only.)

Complex.zero = new Complex(0,0);

Complex.one = new Complex(1,0);

Complex.i = new Complex(0,1);
分享到:
评论

相关推荐

    JavaScript 圣经第5版-Javascript编程宝典--黄金版 .rar

    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-verbose-summary-reporter` 是一个专为前端开发者设计的开源库,它与 Karma 集成,提供了更详尽的测试结果摘要报告。Karma 是一个流行的自动化测试运行器,...

    github-profile-summary-cards:生成个人档案自述文件的github摘要卡的工具

    将GITHUB_TOKEN替换为您的回购密钥并触发操作后,您可以使用profile-summary-card-output文件夹中的所有内容。添加到现有存储库添加此操作以回购,并将yml文件中的GITHUB_TOKEN替换为您的回购密码。GitHub动作用法...

    javascript-advanced-summary

    2. **闭包**:闭包是JavaScript中的一个重要概念,它允许函数访问并操作其外部作用域的变量,即使在其外部函数已经执行完毕后。闭包常用于数据封装和私有变量,也可以创建内存泄漏,因此需谨慎使用。 3. **异步编程...

    JavaScript - JavaScript Tutorial

    - **JS Summary**:总结JavaScript的核心概念和技术要点。 #### JS示例 - **JS Examples**:提供超过200个示例代码,涵盖从简单到复杂的各种应用场景。 - **JS Objects Examples**:面向对象编程的示例,展示如何...

    summary-20230720.zip

    通常,"summary-20230720.zip"这样的文件名可能表示这是一个包含2023年7月20日总结或报告的压缩文件。如果这是一份关于IT行业的总结,可能涵盖了技术趋势、最新产品发布、行业新闻、代码示例或项目更新等内容。 ...

    lectures_summary-源码.rar

    【标题】"lectures_summary-源码.rar" 提供的是一种学习资源的压缩包,很可能包含一系列编程课程的源代码。从"source code"这个词我们可以推测,这份资料可能与编程教学或者软件开发有关,可能是为了帮助学生或...

    ant - build.xml - summary

    标题中的“ant - build.xml - summary”指的是Apache Ant工具中的构建文件`build.xml`的概要。Apache Ant是一个广泛使用的Java项目自动化构建工具,类似于Unix的make,但基于XML。在Ant中,`build.xml`是项目的配置...

    gitbook-plugin-summary:Gitbook插件自动生成SUMMARY.md

    gitbook-plugin-summary Gitbook插件自动生成SUMMARY.md介绍这个插件是出于无法从基本树结构自动生成SUMMARY.md文件的挫败感而创建的。 结果是你安装了这个插件,它就在你当前的book.json文件book.json 。 无需...

    jest-summary-reporter

    配置除了Jest提供的默认报告程序之外,还添加jest-summary-reporter : { "reporters": [ "default", "jest-summary-reporter" ]}如果您还想查看通过/未决的测试,请执行以下操作: { "reporters": [ "default", [...

    Summary---An-Chrome-Extension:当鼠标悬停在网站链接上时显示摘要的扩展

    总结---An-Chrome-Extension 当鼠标悬停在网站链接上时显示摘要的扩展要使用此扩展程序,您需要有 chrome。 与此扩展一起安装后,当您将鼠标移到链接上时,将显示一个附加窗口。 该窗口将显示链接的基本信息,即最大...

    summary-widget:将您的CodersRank个人资料摘要集成到您的个人网站

    npm i @codersrank/summary --save 安装后,您需要导入和注册Web组件: import CodersrankSummary from '@codersrank/summary' ; // register web component as <codersrank> element window . customElements . ...

    Javascript-Summary:Javascript摘要

    JavaScript연산자연산자연산자메소드중중중토토토토토토토토토향향향그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을원함원함원함 下载ECMAScript第5版(2009年...

    summary-news-app:AI汇总的新闻应用程序

    【标题】"summary-news-app:AI汇总的新闻应用程序"是一个基于现代Web技术和人工智能技术构建的项目,旨在为用户提供高效、个性化的新闻阅读体验。通过利用先进的自然语言处理(NLP)算法,特别是BERT模型,该应用...

    gulp-file-summary:Gulp插件以获取所有文件的摘要

    > npm install gulp-file-summary --save-dev用var fileSummary = require ( 'gulp-file-summary' ) ;gulp . task ( 'summary' , function ( ) { return gulp . src ( './lib/*.js' ) . pipe ( fileSummary ( { ...

    一个为您生成个人档案自述文件的github摘要卡的工具-JavaScript开发

    Github配置文件摘要卡繁体中文此存储库受profile-summary-for-github的启发,此操作将生成github配置文件摘要卡并推送到您的存储库。 添加此动作后,您也可以自己触发动作。 将其添加到工作流程后,应触发工作流程,...

    Node.js-nod版本的github-profile-summary

    对于 "node-github-profile-summary-master" 这个文件夹名,我们可以推测这是项目源代码的主分支或者是最稳定的版本。一般情况下,GitHub 仓库的默认分支是 `master`,其中包含了项目的主要代码和配置文件。这里可能...

    python-profile-summary-for-github:可视化GitHub摘要配置文件的工具

    `python-profile-summary-for-github` 是一个专为GitHub用户设计的工具,它提供了可视化的配置文件摘要,帮助用户更直观地理解并管理他们的GitHub个人资料。这个工具基于Python编程语言,利用了Flask框架来构建web...

    JavaScript.Application.Design.A.Build.First.Approach

    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 web development workflow....

    Eloquent_JavaScript(JavaScript编程精解)第二版 英文版

    ### Eloquent JavaScript(JavaScript编程精解)第二版 英文版 #### 游戏式编程,例子丰富,属于一本前端进阶,新手需要先打好基础,否则看这书比较费劲 **Eloquent JavaScript** 是一本深受广大前端开发者喜爱的...

Global site tag (gtag.js) - Google Analytics