Home > Web Front-end > JS Tutorial > Common methods for parameter detection when calling javascript function_javascript skills

Common methods for parameter detection when calling javascript function_javascript skills

WBOY
Release: 2016-05-16 18:33:59
Original
1204 people have browsed it
Copy code The code is as follows:

var f1 = function(p1,p2,p3){
switch(arguments.length){
case 0:
alert("no-parameter version of f1")
case 1:
alert("1 parameter version of f 1: " p1)
break;
case 2:
alert("2 parameter version of f1: " p1 ", " p2)
break;
case 3:
alert( "3-parameter version of f1: " p1 ", " p2 ", " p3)
     break;
    Default:
    alert("Calling with more than 3 parameters is not supported!");
                break; 2","3");
f1("1","2","3","4")


2. Parameter number detection
js engine is the same There is no forced check of the number of parameters when the function is called, so you can only handle it yourself. Sample code:



Copy code


Code As follows:
var fnMustOneParam = function(p){ //Check whether there are parameters passed in if (typeof p=="undefined"){ alert("fnMustOneParam must have parameters passed in before calling (1)!");
                                                                                                                                     ; {
      alert("fnMustOneParam must have parameters passed in before calling (2)!");
          return; !=0){
       alert("fnMustOneParam can only be called with one parameter!"); /fnMustOneParam(1,3,4);


3. Parameter basic type detection
The js engine will also not detect the type of parameters, if you want to make some restrictions on the basic types of parameters , you can use typeof to determine the basic type




Copy code


The code is as follows:


var fnString = function (s){
if (arguments.length!=1){
alert("Number of parameters does not match!");
return ;
}
if (typeof s != "string"){
       alert("Only string type parameters can be passed in!"); 🎜>

4. Parameter type detection of custom classes
The method mentioned in Article 3 can only detect the basic type of parameters. If it is a parameter of a custom class, if the typeof operator is used , only the type detection result of object can be obtained. In this case, the instanceof operator symbol can be used to solve the problem Copy code
The code is as follows: >

function Person(name,age){
this.name = name;
this.age = age;
}
function fnPerson(p){
if ( arguments.length=1 && p instanceof Person){
  alert("fnPerson was called successfully, p.name=" p.name ",p.age=" p.age);
  }
 else{
Alert ("You must pass in a parameter of a Person type to call!"); Yang Guo',30))

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template