开发者

jQuery - What does this syntax mean?

开发者 https://www.devze.com 2023-04-07 11:08 出处:网络
I am looking at jquery.ui-1.8.11.js: $.extend(Datepicker.prototype, { /* Class name added to elements to indicate already configured with a date picker. */

I am looking at jquery.ui-1.8.11.js:

$.extend(Datepicker.prototype, {
    /* Class name added to elements to indicate already configured with a date picker. */
    markerClassName: 'hasDatepicker',

    /* Debug logging (if enabled). */
    log: function () {
        if (this.debug)
            console.log.apply('', arguments);
    },

What is开发者_如何学Go that log: function() syntax? What's it called? How can it be used?


The second argument to an $.extend call is an Object. They're defined using curly braces {} and take key: value pairs. The value can be a function, which is what is happening for the log.

This is analogous to the following:

var myObject = {
  prop1: 'some value',
  prop2: 'some other value',
  method1: function () {
    // some code here.
  }
}

You can later call myObject.method1() to perform whatever the code inside the function is.


It is extending the object properties on the Datepicker.prototype object.

For example, after that statement is run, you could do:

 alert(new Datepicker().markerClassName); // alert('hasDatepicker');

 // OR

 new Datepicker().log(); // calls the function

Basically, $.extend() allows you to modify an object with additional properties (or overwrite them). Consider the following object:

 var obj = { Text: 'Hello World' };

 alert(obj.Text); // Hello World

 $.extend(obj, {
     Text: 'Something Else',
     func: function() { alert('Stuff'); }
 });

 alert(obj.Text); // Something Else
 obj.func();


This syntax uses JavaScript prototyping to extend a Datepicker object and add additional attributes and functions. This adds Datepicker.log as a function and Datepicker.markerClassName.


The second argument (begun with an opening curly brace { and ended with a closing curly brace }) to the $.extend() call is an Object literal. Generally speaking, it's shorthand for explicitly creating an object with new Object. These are roughly equivalent:

var foo = new Object;
foo.bar = 'bar';

and

var foo = {
    bar: 'bar'
};

It's also worth noting that the way functions are assigned, they are anonymous functions being assigned as values to properties of the object. As in:

var foo = new Object;
foo.method1 = function() { ... };

It is also possible to use named functions:

function fn1() { ...}
foo.method2 = fn1;

Or:

foo.method3 = function fn2() { ... };

Or:

var foo = {
    method4: function fn3() { ... }
};

There are other literals useful for shorthand in JavaScript, such as the Array literal:

var arr = ['members', 'are', 'separated', 'by', 'commas'];

And the RegExp literal (note that parsing a RegExp literal does not have the same escaping rules as passing a string to new RegExp()):

var exp = /some pattern/g; // Modifiers are after the closing slash
0

精彩评论

暂无评论...
验证码 换一张
取 消