简体   繁体   English

需要帮助来理解JavaScript中“:”的用法

[英]Need help to understand the usage of “:” in JavaScript

I'm using jQuery validate plugin, and when I met the following script, i recognize, that i don't understand many things in jQuery:) 我正在使用jQuery validate插件,当我遇到以下脚本时,我认识到,我在jQuery中不了解很多东西:)

Look please 请看

$("#invitationform").validate({
        rules: 
        {
            phone: 
  • List item 项目清单

      { required: true, minlength: 6, number:true } }, messages: { phone: { required: "Please enter a phone number", minlength: "Your number must consist of at least 6 digits" } } }); 

Please help me to understand who are "rules" , and "phone" here? 请帮助我了解谁是“规则”“电话”在这里? Are they something like list elements, or they are variables of the object? 它们是列表元素,还是对象的变量? And why we call them via : ? 为什么我们通过以下方式称呼它们?

Short explanation, or links on some documentation will be very nice. 简短的解释,或一些文档的链接将是非常好的。

Thanks much 非常感谢

Apart from the few primitive types (numbers, strings, booleans, null and undefined) everything is an object in JavaScript (even functions). 除了少数基本类型(数字,字符串,布尔值,空和未定义)之外,一切都是JavaScript(甚至是函数)中的对象。

Objects are basically containers of properties, which happen to be very useful for collecting and organizing data. 对象基本上是属性的容器,这对于收集和组织数据非常有用。

One popular method to create objects is to use the object literal notation , where the property name is separated from the value by the colon : symbol: 创建对象的一种流行方法是使用对象文字表示法 ,其中属性名称通过冒号:符号与值分隔:

var myFirstObject = {
   'name': 'Bobby',
   'surname': 'Smith'
};

The quotes around property names are optional if the name would be a legal JavaScript identifier and not a reserved word. 如果名称是合法的JavaScript标识符而不是保留字,则属性名称周围的引号是可选的。 A property's name can be any string. 属性的名称可以是任何字符串。 Objects can contain other objects, so they can easily represent trees or graphs: 对象可以包含其他对象,因此它们可以轻松表示树或图形:

var myFlight = {
   'airline': 'Airline Name',
   'number': 'AN700',
   'departure': {
      'IATA': 'SYD',
      'time': '2010-09-04 23:10:00'
   },
   'arrival': {
      'IATA': 'LAX',
      'time': '2010-09-05 05:14:00'
   }      
};

JavaScript objects also happen to be a convenient hash table data structure. JavaScript对象也恰好是一种方便的哈希表数据结构。 You could easily do the following: 您可以轻松执行以下操作:

var myHashTable = {};
myHashTable['name'] = 'Bobby';
myHashTable['surname'] = 'Smith';
// subscript notation:
alert(myHashTable['name'] + ' ' + myHashTable['surname']);
// dot notation: (equivalent)
alert(myHashTable.name + ' ' + myHashTable.surname);

jQuery, and many other JavaScript libraries, often expect an object as an argument to a method. jQuery和许多其他JavaScript库经常期望一个对象作为方法的参数。 To give you an example from another library, this is how a map is constructed using the Google Maps API v3 : 为了向您提供另一个库的示例,这是使用Google Maps API v3构建地图的方式

var map = new google.maps.Map(document.getElementById('map'), { 
   mapTypeId: google.maps.MapTypeId.ROADMAP,
   center: new google.maps.LatLng(39.904667, 116.408198),
   zoom: 12
});

Note how we can easily pass readable complex structures as function arguments because of how JavaScript works. 请注意,由于JavaScript的工作原理,我们可以轻松地将可读复杂结构作为函数参数传递。

That is not actually specific to jQuery, but is in fact JSON (which is just a part of vanilla JavaScript. As pygorex noted, those are object literals, which is to say that they are the property names of an object. 这实际上并不特定于jQuery,但实际上是JSON(它只是vanilla JavaScript的一部分。正如pygorex所说,那些是对象文字,也就是说它们是对象的属性名称。

That means that if, for example, you were to create an object with that notation, rather than passing it into a function, you would would be able to call on the values they contain. 这意味着,例如,如果您要使用该表示法创建对象,而不是将其传递给函数,您将能够调用它们包含的值。 For example, if you did this: 例如,如果你这样做:

var options = {
    rules: 
    {
        phone: 
        {
            required: true,
            minlength: 6,
            number:true
        }
    },
    messages: 
    {
        phone: 
        {
            required: "Please enter a phone number",
            minlength: "Your number must consist of at least 6 digits"
        }
    }
};

You could then do something like this: 然后你可以做这样的事情:

var isPhoneRequired = options.rules.phone.required;

jQuery uses this structure a lot in order to allow for a convenient method of passing a multitude of parameters into a plug-in function. jQuery经常使用这种结构,以便能够方便地将大量参数传递给插件函数。 It is convenient because you can pass in many options while only specifying one parameter in the function call, and you can easily set defaults which are overridden only if an alternate value is specified by the caller. 这很方便,因为您只能在函数调用中指定一个参数时传入许多选项,并且只有在调用者指定了备用值时,您才能轻松设置被覆盖的默认值。

rulesphoneJavascript对象文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM