jQuery 函数声明说明

发布于 2024-12-20 21:04:29 字数 292 浏览 6 评论 0原文

我已经打开 jQuery 1.7.1 库并想研究代码,但我发现函数以奇怪的方式声明(对我来说)。例如:

show: function() {
        //some code here
},

我学会了以这种方式定义函数:

function show() {
  //some code here
}

有人可以解释一下为什么 show 函数不是用第二种方式编写的(就像互联网上的大多数教程一样)?

I've opened jQuery 1.7.1 library and wanted to study the code, but I've found a that functions are declared in strange way (for me). For example:

show: function() {
        //some code here
},

I learned to define function on this way:

function show() {
  //some code here
}

Can someone explain me why show function is not written on second way (like in most tutorials on internet)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

峩卟喜欢 2024-12-27 21:04:29

这是因为它位于对象内。 对象文字有它们的以这种方式定义的属性:

{
    name: value,
    //OR
    'name': value
}

其中值几乎可以是任何内容,例如数字、字符串、函数,甚至另一个对象。在 JavaScript 中,您还可以声明匿名函数并分配他们到一个变量。事实上,以下声明具有相同的效果:

//declares the myFunc1 function
function myFunc1() {}
//declares an anonymous function and assigns it to myFunc2
var myFunc2 = function() {};

//you can now call either like so:
myFunc1();
myFunc2();

因此,如果我有一个对象并且我希望其属性之一是一个函数,则将这两个概念结合起来,我会这样做:

var myObj = {
    name: 'My Object',
    init: function() {
        return 'Initializing!';
    },
    version: 1.0
};

alert(myObj.init());

然后您将得到输出:正在初始化!。请务必查看 Mozilla 开发者网络 上的精彩文档和教程,包括他们的JavaScript 教程系列

希望这会有所帮助!

This is because it is within an object. Object Literals have their properties defined in this way:

{
    name: value,
    //OR
    'name': value
}

Where value can be nearly anything such as a number, string, function, or even another object. In JavaScript you can also declare anonymous functions and assign them to a variable. In fact, the following declarations have the same effect:

//declares the myFunc1 function
function myFunc1() {}
//declares an anonymous function and assigns it to myFunc2
var myFunc2 = function() {};

//you can now call either like so:
myFunc1();
myFunc2();

So, combining those two concepts if I have an object and I want one of its properties to be a function I would do it like so:

var myObj = {
    name: 'My Object',
    init: function() {
        return 'Initializing!';
    },
    version: 1.0
};

alert(myObj.init());

You would then get the output: Initializing!. Make sure to check out the great documentation and tutorials on the Mozilla Developer Network, including their JavaScript Tutorial Series

Hope this helps!

记忆之渊 2024-12-27 21:04:29

第一种编写方式本质上是将函数设置为对象的属性。

例如:

// I can create a new empty object
var myObject = {};

// or I can create a new object with a basic property
var myObject = { 
        color: "blue" 
    };

// I can also create an object with methods (like jQuery)
var myObject = { 
        color: "blue", 
        showColor: function(){ 
            alert(this.color); 
        } 
    };

// and you can use the object like this
myObject.showColor(); // calls the showColor method and will alert "blue"

这有助于 jQuery 封装、命名空间和组织代码。

这里有一些非常好的文章:

Writing it the first way is in essence, setting a function as property of an object.

For example:

// I can create a new empty object
var myObject = {};

// or I can create a new object with a basic property
var myObject = { 
        color: "blue" 
    };

// I can also create an object with methods (like jQuery)
var myObject = { 
        color: "blue", 
        showColor: function(){ 
            alert(this.color); 
        } 
    };

// and you can use the object like this
myObject.showColor(); // calls the showColor method and will alert "blue"

This helps jQuery to encapsulate, namespace, and organize code.

Here are a couple of pretty good write-ups:

苍景流年 2024-12-27 21:04:29

第一个声明,即 show: function,将 show 定义为具有函数类型的对象中的字段。第二个在当前范围内声明一个名为 show 的函数(可能是全局的?)

The first declaration, i.e., show: function, defines show to be a field in an object having type function. The second declares a function named show within the current scope (possibly global?)

扭转时空 2024-12-27 21:04:29

它们被描述为 js 对象函数。在这种情况下:

var jQuery = {
    show: function(){
        //some code here
    }
}

例如,您可以像 jQuery.show() 一样访问它。

我想说查德的回答对于深入研究来说是最准确的。您应该研究它们,因为它们可以以一种非常干净的方式彻底改变您编写 js 的方式,并且与其他库发生冲突的可能性要小得多。

they're being described as a js object function. In this case:

var jQuery = {
    show: function(){
        //some code here
    }
}

so you access it like jQuery.show() for example.

I would say Chad's answer is most accurate for in depth research. You should look into them as they can revolutionize how you write js in a very clean way that is much less likely to conflict with other libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文