`
ellen_yang
  • 浏览: 21542 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

7.1. Creating Objects 创建对象

阅读更多

Objects are composite datatypes: they aggregate multiple values into a single unit and allow you to store and retrieve those values by name. Another way to explain this is to say that an object is an unordered collection of properties, each of which has a name and a value. The named values held by an object may be primitive values, such as numbers and strings, or they may themselves be objects.

(对象是一种复合的数据类型:它将多种数值聚集在一个单元中,并且允许你通过名字存进和取出这些值,另一种解释它的方式是:一个对象是一个无序的属性组成的集合,每一个属性都会有自己的名字和值。存储在对象中的已命名的值可以是原始值,如 numbers 和strings 也可以是对象)

1.对象之间量方式创建对象

The easiest way to create an object is to include an object literal in your JavaScript code. An object literal is a comma-separated list of property name/value pairs, enclosed within curly braces. Each property name can be a JavaScript identifier or a string, and each property value can be a constant or any JavaScript expression. Here are some examples:

(最简单的创建对象的方式是在你的javascript代码中包含一个对象直接量。对象之间量是由逗号分隔的属性名/值对列表,包含在大括号之内。任何属性名都可以是一个javascript标识符或是一个string,任何一个属性值都可以是一个常量或任意的javaScript 表达式。这里有几个例子)

var empty = {};  // An object with no properties
var point = { x:0, y:0 };
var circle = { x:point.x, y:point.y+1, radius:2 };
var homer = {
    "name": "Homer Simpson",
    "age": 34,
    "married": true,
    "occupation": "plant operator",
    'email': "homer@example.com"
};

An object literal is an expression that creates and initializes a new and distinct object each time it is evaluated. That is, a single object literal can create many new objects if it appears within the body of a loop in a function that is called repeatedly.

(对象直接量是个每次都会创建一个新的不同对象的表达式,也就是说 单个对象直接量可以创建很多新的对象,如果它出现在被重复调用的一个函数 的loop循环体内。)

2.new 操作符创建对象

The new operator can create specialized kinds of objects. You follow it with an invocation of a constructor function that initializes the properties of the object. For example:

var a = new Array(); // Create an empty array
var d = new Date();  // Create an object representing the current date and time
var r = new RegExp("javascript", "i");  // Create a pattern-matching object

 

The Array(), Date(), and RegExp() constructors shown above are a built-in part of core JavaScript. (The Array() constructor is described later in this chapter, and you can look up the others in Part III.) The Object() constructor creates an empty object, just as the literal {} does.

It is also possible to define your own constructor functions in order to initialize newly created objects in whatever way you'd like.

分享到:
评论

相关推荐

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...

    Devart dbForge Studio for MySQL Professional Edition v7.1.13

    Use database diagram for creating, analyzing, reverse-engineering, printing and customizing your MySQL databases and for: Viewing foreign key relations Displaying DB objects with properties Execution...

    ZendFramework中文文档

    2.1.3. 创建访问控制列表(ACL) 2.1.4. 注册角色(Role) 2.1.5. 定义访问控制 2.1.6. 查询 ACL 2.2. 精细的访问控制 2.2.1. 精细的访问控制 2.2.2. 除去访问控制 2.3. 高级用法 2.3.1. 保存 ACL 数据确保持久...

    Python Tutorial 入门指南3.6英文版

    7.1. Fancier Output Formatting 70 7.1.1. Old string formatting 75 7.2. Reading and Writing Files 76 7.2.1. Methods of File Objects 77 7.2.2. Saving structured data with json 79 8. Errors and ...

    Programming with MS VB.NET.pdf

    Contents Introduction Course Materials..................Creating and Destroying Objects...........................................................................16 Demonstration: Creating Classes.....

    SAS_Programming_III英文

    Table of Contents Course Description................................................................................................7.1 Introduction..........................................................

    VB.Net Programming.pdf

    Creating and Destroying Objects.......................................................................16 Demonstration: Creating Classes..................................................................

    MSDN Traning - VB.NET (VBL).pdf

    Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...

    Programming with Microsoft Visual Basic.NET_Delivery Guide.pdf

    Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...

    MySql存储过程编程.chm

    Section 7.1. Creating Stored Programs Section 7.2. Editing an Existing Stored Program Section 7.3. SQL Statements for Managing Stored Programs Section 7.4. Getting Information About Stored ...

    ros by example for indigo volume 2

    4. Creating a URDF Model for your Robot....................................................89 4.1 Start with the Base and Wheels...........................................................................

    Programming Excel With Vba And .net.chm

    Creating Your Own Objects Section 5.1. Modules Versus Classes Section 5.2. Add Methods Section 5.3. Create Properties Section 5.4. Define Enumerations Section 5.5. Raise Events Section ...

    Servlets和JSP核心技术 卷2(英文版) 第一部分

    Section 7.1. Tag Library Components Section 7.2. Example: Simple Prime Tag Section 7.3. Assigning Attributes to Tags Section 7.4. Example: Prime Tag with Variable Length Section 7.5. Including Tag...

    Servlets和JSP核心技术 卷2(英文版) 第二部分

    Section 7.1. Tag Library Components Section 7.2. Example: Simple Prime Tag Section 7.3. Assigning Attributes to Tags Section 7.4. Example: Prime Tag with Variable Length Section 7.5. Including Tag...

    C++标准库(第二版)英文版.pdf

    The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...

    Hibernate Reference Documentation3.1

    15.1. Creating a Criteria instance 15.2. Narrowing the result set 15.3. Ordering the results 15.4. Associations 15.5. Dynamic association fetching 15.6. Example queries 15.7. Projections, aggregation ...

    《activmq in action 》

    2.3.6. Administered Objects ...................................................... 49 2.3.7. Using the JMS APIs to Create JMS Applications ........... 50 2.3.8. Message-Driven Beans .....................

    javascript权威指南(第六版)

    7.1 Creating Arrays 141 7.2 Reading and Writing Array Elements 142 7.3 Sparse Arrays 144 7.4 Array Length 144 7.5 Adding and Deleting Array Elements 145 7.6 Iterating Arrays 146 7.7 Multidimensional ...

    spring-framework-reference4.1.4

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

Global site tag (gtag.js) - Google Analytics