Coffeescript 简明教程
CoffeeScript - Objects
CoffeeScript 中的对象与 JavaScript 中的对象类似。这些是属性的集合,其中属性包括由分号分隔的键和值( ; )。简而言之,CoffeeScript 对象是键值对的集合。使用大括号定义对象,一个空对象表示为 {} 。
Objects in CoffeeScript are similar to those in JavaScript. These are a collection of the properties, where a property includes a key and a value separated by a semi colon (;). In short, CoffeeScript objects are a collection of key-value pairs. The objects are defined using curly braces, an empty object is represented as {}.
Syntax
以下是 CoffeeScript 中对象的语法。在此处,我们将对象的键值对放在大括号中,并使用逗号将其分隔( , )。
Given below is the syntax of an object in CoffeeScript. In here, we place the key-value pairs of the objects within the curly braces and they are separated using comma (,).
object ={key1: value, key2: value,......keyN: value}
Example
以下是 CoffeeScript 中定义对象的示例。将此代码保存在名为 objects_example.coffee 的文件中
Following is an example of defining an object in CoffeeScript. Save this code in a file with name objects_example.coffee
student = {name: "Mohammed", age: 24, phone: 9848022338 }
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
> coffee -c objects_example.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var student;
student = {
name: "Mohammed",
age: 24,
phone: 9848022338
};
}).call(this);
就像在数组中一样,我们可以通过在如下所示的新行中指定键值对来删除逗号。
Just as in arrays, we can remove the commas by specifying the key-value pairs in new lines as shown below.
student = {
name: "Mohammed"
age: 24
phone: 9848022338
}
Indentations instead of curly braces
就像 CoffeeScript 中的其他块语句一样,我们可以使用缩进代替大括号 {} ,如下例所示。
Just like other block statements in CoffeeScript, we can use indentations instead of curly braces {} as shown in the following example.
Nested objects
在 CoffeeScript 中,我们可以在对象中编写对象。
In CoffeeScript, we can write objects within objects.
Example
以下示例演示了 CoffeeScript 中的嵌套对象。将此代码保存在名为 nested_objects.coffee 的文件中
The following example demonstrates the nested objects in CoffeeScript. Save this code in a file with name nested_objects.coffee
contact =
personal:
email: "personal@gmail.com"
phone: 9848022338
professional:
email: "professional@gmail.com"
phone: 9848033228
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
> coffee -c nested_objects.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var contact;
contact = {
personal: {
email: "personal@gmail.com",
phone: 9848022338
},
professional: {
email: "professional@gmail.com",
phone: 9848033228
}
};
}).call(this);
Comprehensions over objects
我们可以使用列表解析来遍历对象的元素。遍历对象元素与遍历数组元素相同。在对象中,我们需要检索 key 和值这两个元素,因此我们会使用两个变量。
To iterate over the contents of an object, we can use comprehensions. Iterating the contents of an object is same as iterating the contents of an array. In objects, since we have to retrive two elements keys and values we will use two variables.
Example
以下示例演示了如何使用列表解析来遍历对象元素。将以下代码保存至一个名为 object_comprehensions.coffee 的文件中:
The following is an example showing how to iterate the contents of an object using comprehensions. Save this code in a file with name object_comprehensions.coffee
student =
name: "Mohammed"
age: 24
phone: 9848022338
console.log key+"::"+value for key,value of student
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
> coffee -c object_comprehensions.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var key, student, value;
student = {
name: "Mohammed",
age: 24,
phone: 9848022338
};
for (key in student) {
value = student[key];
console.log(key(+"::" + value));
}
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
> coffee object_comprehensions.coffee
执行后,CoffeeScript 文件产生以下输出。
On executing, the CoffeeScript file produces the following output.
name::Mohammed
age::24
phone::9848022338
Arrays of Objects
在 CoffeeScript 中,数组还可以包含对象,如下所示:
In CoffeeScript, an array can also contain objects in as shown below.
a = [
object1_key1: value
object1_key2: value
object1_key3: value
,
object2_key1: value
object2_key2: value
object2_key3: value
]
以下示例演示如何定义一个对象数组。我们可以仅列出所需对象的关键值对,并使用逗号将其分隔 (,) 。
The following example shows how to define an array of objects. We can just list the key value pairs of the objects we want in an array by separating them using commas (,).
students =[
name: "Mohammed"
age: 24
phone: 9848022338
,
name: "Ram"
age: 25
phone: 9800000000
,
name: "Ram"
age: 25
phone: 9800000000
]
console.log student for student in students
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c array_of_objects.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var i, len, student, students;
students = [
{
name: "Mohammed",
age: 24,
phone: 9848022338
}, {
name: "Ram",
age: 25,
phone: 9800000000
}, {
name: "Ram",
age: 25,
phone: 9800000000
}
];
for (i = 0, len = students.length; i < len; i++) {
student = students[i];
console.log(student);
}
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee array_of_objects.coffee
执行后,CoffeeScript 文件产生以下输出。
On executing, the CoffeeScript file produces the following output.
{ name: 'Mohammed', age: 24, phone: 9848022338 }
{ name: 'Ram', age: 25, phone: 9800000000 }
{ name: 'Ram', age: 25, phone: 9800000000 }
Reserved Keywords
JavaScript 不允许将保留关键字用作对象的属性名,如果我们希望使用它们,则需要使用双引号将其包装起来 " " 。
JavaScript does not allow reserved keywords as property names of an object, if we want use them, we have to wrap them using double quotes " ".
Example
请考虑以下示例。此处,我们创建了一个名为 class 的属性,它是一个保留关键字。将以下代码保存至一个名为 reserved_keywords.coffee 的文件中:
Consider the following example. Here we have created a property with name class, which is a reserved keyword. Save this code in a file with name reserved_keywords.coffee
student ={
name: "Mohammed"
age: 24
phone: 9848022338
class: "X"
}
console.log key+"::"+value for key,value of student
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c reserved_keywords.coffee
在编译时,它会给您以下 JavaScript。您可以在此处观察到,CoffeeScript 编译器已代表我们使用双引号包装了关键字 class。
On compiling, it gives you the following JavaScript. Here you can observe that the CoffeeScript compiler wrapped the keyword class with double quotes on behalf of us.
// Generated by CoffeeScript 1.10.0
(function() {
var key, student, value;
student = {
name: "Mohammed",
age: 24,
phone: 9848022338,
"class": "X"
};
for (key in student) {
value = student[key];
console.log(key + "::" + value);
}
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee array_of_objects.coffee
执行后,CoffeeScript 文件产生以下输出。
On executing, the CoffeeScript file produces the following output.
name::Mohammed
age::24
phone::9848022338
class::X