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.
分享到:
相关推荐
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...
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...
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 数据确保持久...
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 ...
Contents Introduction Course Materials..................Creating and Destroying Objects...........................................................................16 Demonstration: Creating Classes.....
Table of Contents Course Description................................................................................................7.1 Introduction..........................................................
Creating and Destroying Objects.......................................................................16 Demonstration: Creating Classes..................................................................
Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...
Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...
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 ...
4. Creating a URDF Model for your Robot....................................................89 4.1 Start with the Base and Wheels...........................................................................
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 ...
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...
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...
The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...
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 ...
2.3.6. Administered Objects ...................................................... 49 2.3.7. Using the JMS APIs to Create JMS Applications ........... 50 2.3.8. Message-Driven Beans .....................
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 ...
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................