简体   繁体   English

JavaScript Function()和JavaScript对象之间的区别

[英]Difference between a JavaScript Function() and a JavaScript object

I'm trying to understand something in JavaScript which confuses me. 我试图理解JavaScript中的一些让我感到困惑的东西。

Let's say that I want to create a method Guard.ThrowError() in JavaScript, I have 2 approaches for this: 假设我想在JavaScript中创建一个方法Guard.ThrowError() ,我有两种方法:

  1. Using an object: 使用对象:

    This is basically the approach which I found on the net. 这基本上就是我在网上找到的方法。

     var Guard2 = { ThrowIfNull: function() { throw new Error('sdmflsfk'); } }; 
  2. Using a Function: 使用功能:

    When you create a class "Guard" in TypeScript and you let it compile to JavaScript, you get something like this: 当您在TypeScript中创建一个类“Guard”并让它编译为JavaScript时,您会得到如下内容:

     var Guard = (function() { function Guard() { } Guard.ThrowIfNull = function () { throw new Error('sdmflsfk'); }; return Guard; })(); 

Both functions can be called using Guard.ThrowIfNull() . 可以使用Guard.ThrowIfNull()调用这两个函数。 I would like to know what's the difference and when choose I choose approach 1 over approach 2? 我想知道有什么区别,什么时候选择我选择方法1而不是方法2?

Thanks for your valuable feedback. 感谢您的宝贵意见。

Kind regards 亲切的问候

I would like to know what's the difference and when choose I choose approach 1 over approach 2? 我想知道有什么区别,什么时候选择我选择方法1而不是方法2?

The only difference is that Guard is a function in your second example and not a function in your first. 唯一的区别是Guard是第二个示例中的一个函数,而不是第一个函数。 So the second one can be called (in that code it doesn't actually do anything, but it can be called); 所以可以调用第二个(在那个代码中它实际上没有任何事情,但可以调用它); the first can't. 第一个不能。

Functions are objects in JavaScript, proper real objects, and so like all other objects, they can have additional properties added to them like your ThrowIfNull . 函数是JavaScript中的对象,正确的实际对象,因此与所有其他对象一样,它们可以像ThrowIfNull一样添加其他属性。

jQuery is a widely-used example of this: The main function, jQuery (aka $ ) is used for its function-ness: jQuery是一个广泛使用的例子:主要功能, jQuery (aka $ )用于其功能:

$("#foo").on("click", function() { /*...*/ });

...and also for its object-ness, because it has various properties attached to it exactly as you've done with Guard : ...以及它的对象,因为它具有与Guard完成的各种属性:

$.ajax(/*...*/);

Both approach works because a function in javascript is also an object. 两种方法都有效,因为javascript中的函数也是一个对象。 The only difference is that you can call a function myFunction(); 唯一的区别是你可以调用函数myFunction(); and you can't do it with a object. 你不能用一个对象来做。

Function approach is used to create classes in JS. 函数方法用于在JS中创建类。 Using this approach, you can have private and public properties. 使用此方法,您可以拥有私有和公共属性。 This cannot be achieve in object approach. 这在对象方法中无法实现。 All properties are public in object. 所有属性都是公共的。

Sample Example 示例示例

 function testClass(){ // Private member var a = "abc"; var b = "test"; function print(){ console.log(a,b); } return { print: print } } testClass().print(); // testClass().a This will throw error 

Guard2 - your approach 1 - is somehow a 'static' definition of an object. Guard2 - 您的方法1 - 在某种程度上是对象的“静态”定义。 You can't instatiate another object of that type. 您无法实例化该类型的另一个对象。

Guard - your approach 2 - is a mixed: it's a definition of an 'automatic class' Guard which gets instantiated. Guard - 你的方法2 - 是混合的:它是一个'自动类' Guard的定义,它被实例化。 The result is almost the same as you can't instantiate new objects from that as well. 结果几乎相同,因为您也无法从中实例化新对象。

Usually, you would do it like this: 通常,你会这样做:

var Guard = function() {

  function Guard() { };

  Guard.ThrowIfNull = function () {
    throw new Error('sdmflsfk');
  }; 

};

var myGuard = new Guard();

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

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