JavaScript 函数重载和类型检查

  var variable_name;

You can’t use a hyphen, though; it is interpreted as a minus sign. js中变量不能用连字符,

其他面向对象语言如java的一个常见特性是,能够根据传入的不同数量或类型的参数,通过“重载”overload函数来发挥不同的功用。
尽管这个特性在js中没有被直接支持,也有很多办法能够实现。
函数重载必须依赖2件事情:判断传入参数数量的能力和判断传入参数类型的能力。我们先来看看参数的数量。
js的每个函数都带有一个包含所有传给函数的参数的伪数组(pseudo array)所有他并非真正意义的数组(也就是不能修改它,
也不能用push增加元素),但是可以访问其他的元素,也就有.length 属性。

// A simple function for sending a message
function sendMessage( msg, obj ) {
    // If both a message and an object are provided
    if ( arguments.length == 2 )
        // Send the message to the object
        obj.handleMsg( msg );

    // Otherwise, assume that only a message was provided
    else
        // So just display the default error message
        alert( msg );
}

// Both of these function calls work
sendMessage( "Hello, World!" );
sendMessage( "How are you?", window );

sendMessage( "How are you?",{
handleMsg:function(msg){
alert("this is a custom mesaage"+msg);
}
});

// A function that takes any number of arguments and makes
// an array out of them
function makeArray() {
    // The temporary array
    var arr = [];
    
    // Go through each of the submitted arguments
    for ( var i = 0; i < arguments.length; i++ ) {
        arr.push( arguments[i] );
    }

    // Return the resulting array
    return arr;
}

Additionally, there exists another method for determining the number of arguments passed to a function. This particular method uses a little more trickiness to get the job done, however. We take advantage of the fact that any argument that isn’t provided has a value of undefined. Listing 2-6 shows a simple function for displaying an error message and providing a default message if one is not provided.

function displayError( msg ) {
    // Check and make sure that msg is not undefined
    if ( typeof msg == 'undefined' ) {
        // If it is, set a default message
        msg = "An error occurred.";
    }

    // Display the message
    alert( msg );
}

typeof 运算符 返回一个用来表示表达式的数据类型的字符串

typeof operand

typeof (operand)

There are six possible values that typeof returns: object, boolean, function, number, string, and undefined.  

当变量不是object或者array类型是,用typeof应该是最好的。但是对于自定义的对象,比如user就不能用这个方法进行类型检查,因为他只会返回object,很难跟其他的object区分开来

// Check to see if our number is actually a string
if ( typeof num  == "string" )
    // If it is, then parse a number out of it
    num = parseInt( num );

// Check to see if our array is actually a string
if ( typeof arr == "string" )
    // If that's the case, make an array, splitting on commas
    arr = arr.split(",");

第二种检查对象类型的方法,需要引用所有javascript对象都带有的一个的属性,称为构造器(construct)。这一属性引用的是

原来用来构造该对象的那个函数。这种方法的一个例子如下:

// Check to see if our number is actually a string
if ( num.constructor == String )
    // If it is, then parse a number out of it
    num = parseInt( num );

// Check to see if our string is actually an array
if ( str.constructor == Array )
    // If that's the case, make a string by joining the array using commas
    str = str.join(',');

http://www.cnblogs.com/zhengchuyu/archive/2008/07/21/1247764.html

http://www.cnblogs.com/cfanseal/archive/2009/04/30/1447282.html

原文地址:https://www.cnblogs.com/youxin/p/2630706.html