Table of Contents
The most interesting function" >The most interesting function
Function with multiple function names" >Function with multiple function names
No function Overloading" >No function Overloading
The difference between function declaration and function expression" >The difference between function declaration and function expression
Function as value" >Function as value
Properties of function" > Properties of function
Method of function" >Method of function
Home Web Front-end JS Tutorial What are the Function types in js

What are the Function types in js

Oct 20, 2017 am 11:04 AM

The most interesting function

  有意思的根源在于- 函数也是对象,同样有拥有属性和方法。 
  由于函数是是对象,那么函数名也就相当于指向函数对象的指针
 通常函数是由"函数声明"创建:
Copy after login
1 function(sum1,sum2){
2   reutnr sum1 + sum2;  
3 }
Copy after login
  也可以使用"函数表达式"创建:
Copy after login
1  var sum = function(sum1,sum2){
2    return sum1+sum2;
3 };
Copy after login
  还有一种方式使用"构造函数"创建: (但不推荐)
Copy after login
<span style="font-family: "Microsoft YaHei"; font-size: 16px">  //这种方式,会导致两次解析代码,第一次解析常规的代码,第二次解析传入构造函数中的字符串。</span><br/><span style="font-family: "Microsoft YaHei"; font-size: 16px">1 <span style="color: #0000ff">var</span> sum = <span style="color: #0000ff">new</span> Function("sum1","sum2","return sum1+sum2")</span>
Copy after login
  "函数声明"与"函数表达式"的区别在于:
Copy after login
  1. Function expression has no function name

  2. Function expressions need to end with a semicolon

Function with multiple function names

  上面提到,函数是对象,而函数名仅仅是执行函数对象的指针。
  这代表着,通过将函数名赋值给其他变量,其他变量则也可以通过自身调用该函数。  注意: 访问函数对象指针时,则需要带小括号,如上面函数访问 sum 函数的指针
           则需要将 sum 赋值给一个变量,该变量便可拥有指向同等函数的指针。    
Copy after login
eg: var sum1 = sum; // sum(); sum1(); 执行的结果相同
Copy after login
强调: 使用 "函数名+()",代表着调用该函数, 不添加 "()",则代表访问该"指向该函数对象的指针"
        将函数名设置为 null ,则可以使其指向的函数断绝联系。
Copy after login
eg: sum = null; // sum(); 则会报错,提示sum不是一个函数, null代表着空对象指针
Copy after login

No function Overloading

  相同的函数名,后者会替换前者(函数声明或者函数表达式都是如此,这里以函数声明为例子)
Copy after login
eg: function sum(sum1){ return sum1+ 100 }    
function sum(sum2){ return sum2+ 200 }
    sum(100);   // 300;
Copy after login

The difference between function declaration and function expression

  js解析器在对于这两者是由区别的。
  函数声明的函数,在开始执行代码之前,就会通过函数声明提升的过程,读取并将函数声明添加到执行环境中。
  即使函数在调用函数后面,也不会影响函数求值。
  而函数表达式则不会,解析器必须执行到当前代码块才会被初始化并执行。如果调用在前,则
Copy after login

Function as value

  在JS中,函数名本身就是变量。所以函数也可以作为值来使用。
  也就是说,不仅仅可以作为一个函数的参数,也可以作为一个函数的返回值。
  而sort() 方法只能根据 tostring() 的值来排序
Copy after login
eg: var data = [{name:&#39;A&#39;,age:2},{name:&#39;B&#39;,age:1}]
  // 创建指定属性的函数
   function setAttr(name){
        return function(o1,o2){
            var v1 = o1[name];
            var v2 = o2[name];
        
            if(v1 < v2) { return -1;
          }
            else if(v1 > v2) { return 1;  }
            else             { return 0;  }
        }
    }
    //使用sort()方法调用
    data.sort(setAttr(&#39;age&#39;));   // [{name,:&#39;B&#39;,age:&#39;1&#39;},{name:&#39;A&#39;,age:&#39;2&#39;}]
    data.sort(setAttr(&#39;name&#39;));  //  [{name:&#39;A&#39;,age:&#39;2&#39;},{name,:&#39;B&#39;,age:&#39;1&#39;}]
Copy after login

Properties of function

  每个函数都包含两个属性: length, prototype;  length : 表示函数接收的参数个数。
Copy after login
1 eg: funciont sum(test){}   //sum.lenght  == 1;
Copy after login
<span style="font-family: "Microsoft YaHei"; font-size: 16px"><span style="background-color: #ffffff"><strong>   prototype:  </strong></span>在JS中,该属性是保存所有实例方法的真正所在。
  在创建自定义引用类型以及实现继承时,该属性是非常重要的。<strong><span style="background-color: #ffffff">   prototype: </span></strong>属性是不可枚举的,因此不能使用for in发现。<br/>  通过构造函数的 <strong><span style="background-color: #ffffff">prototype </span></strong>属性,创建的方法或者属性,都会被它的实例对象所继承,从而实现继承机制。<br/></span>
Copy after login

Method of function

  每个函数都包含两个非继承而来的方法: apply() 与 call();
  两者区别在于,第二个参数的不同。
  该方法强大在于,可以扩充函数赖于运行的作用域。  apply(): 第一参数是在其中运行函数的作用域,另一个是参数数组。  call()   : 第一参数是在其中运行函数的作用域,其他参数直接传递给函数,
              也就是除第一个参数,其他都是逐个列举出来。
 使用场景: 两者结果没有任何区别,而具体使用哪个,则取决于你传入的参数使用哪个方法方便。
                如果传入参数是一个数组则使用 apply 更合适一点,否则 call() 更合适一点。
Copy after login

The above is the detailed content of What are the Function types in js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to create a video matrix account? What types of matrix accounts do it have? How to create a video matrix account? What types of matrix accounts do it have? Mar 21, 2024 pm 04:57 PM

With the popularity of short video platforms, video matrix account marketing has become an emerging marketing method. By creating and managing multiple accounts on different platforms, businesses and individuals can achieve goals such as brand promotion, fan growth, and product sales. This article will discuss how to effectively use video matrix accounts and introduce different types of video matrix accounts. 1. How to create a video matrix account? To make a good video matrix account, you need to follow the following steps: First, you must clarify what the goal of your video matrix account is, whether it is for brand communication, fan growth or product sales. Having clear goals helps develop strategies accordingly. 2. Choose a platform: Choose an appropriate short video platform based on your target audience. The current mainstream short video platforms include Douyin, Kuaishou, Huoshan Video, etc.

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

What is the type of return value of Golang function? What is the type of return value of Golang function? Apr 13, 2024 pm 05:42 PM

Go functions can return multiple values ​​of different types. The return value type is specified in the function signature and returned through the return statement. For example, a function can return an integer and a string: funcgetDetails()(int,string). In practice, a function that calculates the area of ​​a circle can return the area and an optional error: funccircleArea(radiusfloat64)(float64,error). Note: If the function signature does not specify a type, a null value is returned; it is recommended to use a return statement with an explicit type declaration to improve readability.

How to use JS and Baidu Maps to add custom location markers to the map How to use JS and Baidu Maps to add custom location markers to the map Nov 21, 2023 am 11:58 AM

How to use JS and Baidu Map to implement the function of adding custom place markers to the map. Introduction: Baidu Map is a very commonly used map service. It provides a wealth of map display and interactive functions, including adding custom place markers. Using JS and Baidu Map API, we can easily implement the function of adding custom location markers on the map. The following is a specific code example: Step 1: Preparation First, import the Baidu Map API file in your HTML file, as follows Shown: &lt;scripttype

See all articles