你不能有一个匿名函数。您需要提供函数名称或将函数分配给变量。
方法 1:将函数分配给变量
$(document).ready(function(){
var abc = function(dataObj){
$.each(dataObj, function(n){
if(dataObj.stream=="null"){
var channelName=dataObj.display_name;
}else{
var channelName=dataObj.stream.display_name;
}
});//each bracket
}
abc();
});
这里有一个例子https://jsfiddle.net/andnLfbx/
$(document).ready(function(){
var abc = function(dataObj){
console.log("abc");
$.each(dataObj, function(n){
if(dataObj.stream=="null"){
var channelName=dataObj.display_name;
}else{
var channelName=dataObj.stream.display_name;
}
});//each bracket
}
abc();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
方法 2:提供函数名称
$(document).ready(function(){
function abc(dataObj){
$.each(dataObj, function(n){
if(dataObj.stream=="null"){
var channelName=dataObj.display_name;
}else{
var channelName=dataObj.stream.display_name;
}
});//each bracket
}
});
举个例子
$(document).ready(function(){
function abc(dataObj){
console.log("abc");
$.each(dataObj, function(n){
if(dataObj.stream=="null"){
var channelName=dataObj.display_name;
}else{
var channelName=dataObj.stream.display_name;
}
});//each bracket
}
abc();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
资源 :
基础知识 :
https://www.w3schools.com/js/js_function_definition.asp
https://www.w3schools.com/js/js_functions.asp
深度知识
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions