Home Web Front-end JS Tutorial js 覆盖和重载 函数_javascript技巧

js 覆盖和重载 函数_javascript技巧

May 16, 2016 pm 06:45 PM
cover Overload

学过JAVA的人对函数的覆盖和重载肯定是再熟悉不过了。
重载指两个或多个函数的参数类型,顺序和数量以及返回值不一样。
覆盖指两个或多个函数的参数类型,顺序和数量以及返回值完全一样。
那javascript真的有这种特性么?
回答是JS中函数重名只会采用最后一个定义。
首先来看下下面的代码

复制代码 代码如下:




New Document








    
    



2th test


    
    



3th test


    





首先按名为 function with no arguments 的按钮

页面的结果为 this is a function named 'testFun' with one argument,the argument is undefined
然后按名为 function with one argument test 的按钮
页面的结果为 this is a function named 'testFun' with one argument,the argument is test
然后按名为 function with no arguments 的按钮
页面的结果为 this is a function named 'testFun2' with no arguments.
然后按名为 function with one argument test 的按钮
页面的结果为 this is a function named 'testFun2' with no arguments.

从以上的测试中我们发现我们只是点换了两个函数的定义顺序,结果大不相同。
从上面的测试中我们可以得出结论: 重载的话,只要函数定义在下面就会覆盖上面的函数定义。
好了,接下来看覆盖。
按名为 test function share the same name and arguments. 的按钮

页面的结果为 this is a function named 'testFun3' second.
测试结果很明显,结论也是和上面相同的。
最终,我们得出结论:
方法重名,JS会以最后定义的函数作为函数体。当然这不包括JS中的继承中的覆盖。
欢迎拍砖
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CentOS overlay installation and CentOS7 overlay installation CentOS overlay installation and CentOS7 overlay installation Feb 29, 2024 pm 10:00 PM

Table of Contents Introduction to this article: CentOS overlay installation CentOS7 overlay installation is shared with you. In the CentOS operating system, it is often necessary to perform overlay installation to solve system problems or perform updates. This article will introduce in detail the steps of CentOS overlay installation and CentOS7 overlay installation to help you effectively manage and maintain the system. CentOS overlay installation When the system is already installed, CentOS overlay installation is usually done to fix system problems or perform updates. Before performing this operation, be sure to back up important data to avoid losing it. 1. Download the latest CentOS image file and make a bootable USB flash drive or CD. 2. Insert the boot USB flash drive or CD into the computer, restart the computer and enter the BIOS settings.

How to overload golang functions? How to overload golang functions? Apr 28, 2024 am 09:27 AM

Traditional function overloading is not supported in Go, but it can be simulated by the following techniques: Multiple return values: Functions with the same method signature but different return types can be overloaded. Variadics: Use the ... syntax to create functions that receive a variable number of arguments, allowing method calls to be handled with different signatures.

What is the difference between golang function overloading and polymorphism? What is the difference between golang function overloading and polymorphism? Apr 30, 2024 am 09:30 AM

Function overloading is not supported in Go language because it adopts duck typing and determines the value type based on the actual type. Polymorphism is achieved through interface types and method calls, and objects of different categories can respond in the same way. Specifically, by defining interfaces and implementing these methods, Go language can make objects of different types have similar behaviors, thereby supporting polymorphism.

Does PHP function support function overloading and function overriding? Does PHP function support function overloading and function overriding? Apr 19, 2024 am 10:06 AM

The PHP language does not support function overloading and function coverage because function overloading may cause ambiguity. Alternative: Use namespaces to isolate functions. Set parameter default values. Use variadic function arguments.

How to implement polymorphism and overloading in Go language? How to implement polymorphism and overloading in Go language? Jun 10, 2023 am 10:25 AM

As a statically typed language, Go language does not seem to be able to implement polymorphism and overloading like dynamic languages. However, the Go language uses the characteristics of interfaces to achieve polymorphism, and the implementation of overloading is simpler and more accurate. Methods to implement polymorphism The interface in the Go language can implement polymorphism during the calling process. The interface can describe the behavior of an object. Any type that implements all methods of the interface can be called an instance of the interface type. In this way, polymorphism can be achieved by simply defining the interface type and implementing different concrete types. Below is a

What are the differences between nosql and mysql What are the differences between nosql and mysql Jan 28, 2023 pm 04:51 PM

Differences: 1. MySQL is a relational database, while NoSQL is non-relational. 2. MySQL’s strict mode restrictions are not easy to expand, while NoSQL is easy to expand. 3. MySQL requires a detailed database model before creating a database, which is not required in NoSQL. 4. MySQL provides a large number of reporting tools, but nosql does not. 5. Compared with MySQL, NoSQL provides a more flexible design. 6. The standard language used in MySQL is SQL, while NoSQL lacks a standard query language.

Java Error: Method override error, how to fix it Java Error: Method override error, how to fix it Jun 24, 2023 pm 11:09 PM

Java, as a popular programming language, is often used to develop various types of applications. However, there are many common errors and exceptions that are often encountered in Java, and one of the most common errors is "Method Override Error". This article will describe the causes of this error and provide some workarounds to avoid such errors. Definition of method overriding In Java, method overriding refers to the process of overriding the defined methods of the parent class in a subclass. When child class methods have the same name, parameter list, and return type as the parent class method, we say they have

Why does an exception occur when a subclass calls an overloaded method? Why does an exception occur when a subclass calls an overloaded method? Mar 08, 2024 pm 04:48 PM

Why does an exception occur when a subclass calls an overloaded method? In object-oriented programming, overloading refers to defining multiple methods in the same class with the same name but different parameter lists to achieve different functional implementations. When a subclass inherits from a parent class, if there is an overload of a method with the same name in the subclass, exceptions may occur. To explain more specifically why an exception is thrown when a subclass calls an overloaded method, let's look at the following code example: classParent{voiddispla

See all articles