简体   繁体   English

Javascript,在函数中将类型作为参数传递

[英]Javascript, pass type as parameter in functions

I am currently learning Javascript and I noticed something that, to me, doesn't make much sense.我目前正在学习 Javascript,我注意到一些对我来说没有多大意义的东西。

In an example on the ArcGIS website, there is this piece of code在 ArcGIS 网站上的一个示例中,有一段代码

var map
require(["esri/map", "dojo/domReady!"], function(Map) {
  map = new Map("mapDiv", {
    center: [-56.049, 38.485],
    zoom: 3,
    basemap: "streets"
  });
});

I don't get how you can do "new Map" when Map is the parameter of function(Map).当 Map 是函数(Map)的参数时,我不明白你如何做“新地图”。 To be able to use new, then Map must be a type and I haven't seen a type being a parameter in other languages.为了能够使用 new,Map 必须是一种类型,我还没有看到其他语言中的类型是参数。

The funny thing about Javascript is that there really are no classes. Javascript 的有趣之处在于它真的没有类。 The equivalent of a class in Javascript is just a special kind of function. Javascript 中类的等价物只是一种特殊的函数。 Actually, forget about the "special" in the previous sentence (just wanted to soften the blow).其实,忘记上一句中的“特别”(只是想减轻打击)。 Any function can "act as a class" and be instantiated with the new operator (although it is really only useful when that function assigns stuff to this so the generated object gains public methods and attributes).任何函数都可以“充当类”并使用new运算符进行实例化(尽管它实际上仅在该函数将内容分配给this以便生成的对象获得公共方法和属性时才有用)。

And functions in Javascript are first-class citizens. Javascript 中的函数是一等公民。 Functions can be assigned to variables and functions can be passed as arguments to other functions.函数可以分配给变量,函数可以作为参数传递给其他函数。 This makes it perfectly legal to pass a function to another function and then have that other function instantiate it with new .这使得将一个函数传递给另一个函数然后让另一个函数使用new实例化它是完全合法的。

Yes, this is indeed a very strange concept to wrap a head around which is used to class-oriented programming languages.是的,这确实是一个非常奇怪的概念,将其用于面向类的编程语言。 But it becomes surprisingly intuitive once you understand it.但是一旦你理解它,它就会变得非常直观。

So what happens in the code you posted in the question is this:那么您在问题中发布的代码中会发生什么:

  1. When require is executed, it calles an anonymous function passing a class function to itrequire被执行时,它调用一个匿名函数传递一个 函数给它
  2. the anonymous function assigns that class function to a local variable Map匿名函数将该 函数分配给局部变量Map
  3. the anonymous function then creates an instance of that class function.然后匿名函数创建 该类 函数的实例。
  4. the instance is assigned to the global variable map实例被分配给全局变量map

To be able to use new, then Map must be a type为了能够使用 new,那么 Map 必须是一个类型

No. For new to work, Map must be a function .不。要让new工作, Map必须是一个function

Functions are first class objects in JavaScript and can be treated like any other piece of data (assigned to variables, passed as function arguments, etc).函数是 JavaScript 中的第一类对象,可以像任何其他数据一样对待(分配给变量,作为函数参数传递等)。

 function One() { this.foo = 1; } function myCallingThing(arg) { alert(new arg().foo + 1); } var Two = One; var instance = new Two; alert(instance.foo); myCallingThing(Two);

This is not unique to JavaScript.这不是 JavaScript 独有的。

#!/usr/bin/perl

use strict;
use warnings;
use v5.10;

sub example {
    say "Hello";
}

sub callme {
    my $var = shift;
    $var->();
}

callme(\&example);

or或者

#!/usr/bin/python

def example():
    print "Hello\n"

def callme(arg):
    arg();

callme(example)

By defining function you can create something like class通过定义函数,您可以创建类似类的东西

function Apple (type) {
    this.type = type;
    this.color = "red";
    this.getInfo = getAppleInfo;
}

Then you can create new instance of your function/class然后你可以创建你的函数/类的新实例

var apple = new Apple('lobo');

So your Map parameter is a function that define objects所以你的Map参数是一个定义对象的函数

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

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