通过prototype定义了数组方法,这样就可以在任意数组调用contains方法
Array.prototype.contains = function ( needle ) { for (i in this) { if (this[i] == needle) return true; } return false; }
例子:
var x = Array(); if (x.contains('foo')) { // do something special }
angularjs运用:
$watch监控"dataList.car_type"的值变化
$scope.carList = false; $scope.$watch("dataList.car_type", function (newValue, oldValue) { if ($scope.dataList.car_type.contains('car') ) { $scope.carList = true; } if($scope.dataList.car_type.length !=0) { $(".personInput").css("color","#9e9e9e"); $(".personInput input").attr("disabled","disabled"); }else { $(".personInput").css("color","#444444"); $(".personInput input").removeAttr("disabled"); } },true);