[英]jQuery Plugin Function Returning Undefined For The Value
我创建了一个jQuery
插件来处理Cookie。 我遇到的麻烦是我创建的用于读取cookie并返回值的函数。 不管我做什么,当我从主页调用函数时
test = $.cookie({
action:'get',
cookie_name:LOGIN_COOKIE_NAME
});
返回值始终是不确定的。 控制台输出如下:
Value found is => 10
Returned => undefined
我尽力想弄清楚这里发生了什么。 我已经尝试了所有我想做的事情,并且花了数小时来搜索,没有解决方案。 以下是我的jQuery插件中的代码:
//获取所需cookie的值
function get_cookie(){ var value = ''; // Get the name to search for var name = settings.cookie_name; // Divide the cookie string into an array var ca = document.cookie.split(';'); // Loop through each element in the main array we just created match_found = false; for(var i=0; i<ca.length; i++){ // Clear the whitespace from the ends to prevent mismatches and split each > element into the cookie name and value var temp = ca[i].trim().split('='); // Now check to see if this is the cookie we are looking for if(temp[0] == name && !match_found){ match_found = true; // If it is, then return the cookie's value cookie_value = temp[1]+''; if(settings.action != 'check'){ value = cookie_value; }else{ value = true; } } } if(!match_found && settings.action == 'check'){ return false; }else if(!match_found && settings.action != 'check'){ return 'dooby'; }else{ console.log('Value found is => '+value); return value; } }
是否有人对导致此问题的原因以及如何纠正它有任何见解?
这是我的jQuery
插件的完整代码
(function($){
$.cookie = function(options) {
// Default Settings
var settings = $.extend({
action : 'check', // create or delete
cookie_name : 'cookie',
cookie_value : 'default',
expiration : 'Sun, 31 Dec 2017 12:00:00 GMT',
path : '/'
}, options);
switch(settings.action){
case "create":
create_cookie();
break;
case "delete":
delete_cookie();
break;
case "check":
return check_for_cookie();
break;
case "get":
get_cookie();
break;
default:
get_cookie();
break;
}
// Create the cookie
function create_cookie(){
try{
document.cookie = settings.cookie_name+"="+settings.cookie_value+"; expires="+settings.expiration+"; path="+settings.path;
console.log("Cookie Created");
}catch(e){
console.log(e);
}
}
// Delete said cookie
function delete_cookie(){
document.cookie = settings.cookie_name+"="+settings.cookie_value+"; expires=Sun, 29 Dec 2013 12:00:00 GMT; path="+settings.path;
}
// Get the value of the sought after cookie
function get_cookie(){
var value = '';
// Get the name to search for
var name = settings.cookie_name;
// Divide the cookie string into an array
var ca = document.cookie.split(';');
// Loop through each element in the main array we just created
match_found = false;
for(var i=0; i<ca.length; i++){
// Clear the whitespace from the ends to prevent mismatches and split each element into the cookie name and value
var temp = ca[i].trim().split('=');
// Now check to see if this is the cookie we are looking for
if(temp[0] == name && !match_found){
match_found = true;
// If it is, then return the cookie's value
cookie_value = temp[1]+'';
if(settings.action != 'check'){
value = cookie_value;
}else{
value = true;
}
}
}
if(!match_found && settings.action == 'check'){
return false;
}else if(!match_found && settings.action != 'check'){
return 'dooby';
}else{
console.log('Value found is => '+value);
return value;
}
}
// Check to see if said cookie exists
function check_for_cookie(){
var is_set = get_cookie(settings.cookie_name);
if(is_set){
return true;
}else{
return false;
}
}
};
}(jQuery));
解:
感谢下面的OJay敏锐的眼光和解决方案。 向下滚动并给他一些投票。
在主switch语句中对get_cookie()
的调用get_cookie()
该调用处理了要执行的操作)没有返回任何内容。 因此,遵循OJay的解决方案,它现在可以像下面这样工作:
case "get":
return get_cookie();
break;
在Cookie“插件”中,您对操作执行了case语句,并调用了相应的private方法。 现在,get方法返回一个值,但是您并未对其执行任何操作,get函数的返回值未由父函数返回
即
switch(settings.action){
///... snipped for brevity
case "get":
//the get_cookie returns something, but you are not returning it past here
get_cookie();
break;
///... snipped for brevity
}
这意味着父函数($ .cookie)中没有用于获取的return语句,因此返回未定义(默认javascript行为)
返回get操作的值,就像您对"check"
操作所做的一样。
它也没有"create"
或"delete"
的返回值,但是它们不一定需要。 尽管我可能会返回一个布尔值(true或false)以指示创建或删除成功
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.