2015-08-03 42 views

回答

2

這是一個常用的JavaScript構造函數,它具有函數中未指定參數的默認值。

這相當於:

if (!input) { 
    input = ' '; 
} 

或者

input = input ? input : ' '; 

或者更詳細:

if (input === null || input === undefined || input === 0 || input === "" || input === false) { 
    input = ' '; 
} 

因此,在這種情況下,萬一input分配空間是一個空字符串或未定義/指定。

0

在Angularjs,這syntaxe被檢查,如果該輸入是空的。如果input爲空,則該值將是「否則它將是input

此語法可以避免nullundefined值。從角DOC(可能是在你看到這句法的例子)

例子:

angular.module('myReverseFilterApp', []) 
.filter('reverse', function() { 
    return function(input, uppercase) { 
    input = input || ''; 
    var out = ""; 
    for (var i = 0; i < input.length; i++) { 
     out = input.charAt(i) + out; 
    } 
    // conditional based on optional argument 
    if (uppercase) { 
     out = out.toUpperCase(); 
    } 
    return out; 
    }; 
}) 
.controller('MyController', ['$scope', function($scope) { 
    $scope.greeting = 'hello'; 
}]); 

它基本上把一個空字符串,而不是一個null值,避免空,如果你不鍵入任何東西示例程序。

+0

感謝您的回答。我看到了先前的帖子,這就是爲什麼我標記它:) – dlearner