如何调用JS的函数

fieldisme 2017-02-07 07:10:56
我想要调用light_star()函数,请问
document.app.light_star(4);
这样调用对吗,为什么出错呢

以下是JS文件中的代码

(function(){
var tools={
getCookie: function(name){
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;

},
setcookie:function(cookieName, cookieValue, seconds, path, domain, secure) {
var expires = new Date();
expires.setTime(expires.getTime() + seconds);
document.cookie = cookieName + "="+ escape (cookieValue) + ";expires=" + expires.toGMTString();
},
trim:function (s){ //去除字符串两边空格
return s.replace(/(^\s*)|(\s*$)/ig, "");
},
// 将HTML转义为实体
escape:function(html) {
var keys = Object.keys || function(obj) {
obj = Object(obj)
var arr = []
for (var a in obj) arr.push(a)
return arr
}
var invert = function(obj) {
obj = Object(obj)
var result = {}
for (var a in obj) result[obj[a]] = a
return result
}
var entityMap = {
escape: {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'/':'\/'
}
}
entityMap.unescape = invert(entityMap.escape)
var entityReg = {
escape: RegExp('[' + keys(entityMap.escape).join('') + ']', 'g'),
unescape: RegExp('(' + keys(entityMap.unescape).join('|') + ')', 'g')
}
if (typeof html !== 'string') return '';
return html.replace(entityReg.escape, function(match) {
return entityMap.escape[match]
})
}
};
var app={
init:function(){
this.getDom();
this.addEvent();
if(this.$comment_con.children('li').length < 1){
this.first=true;
this.$comment_con.after('<div class="page-unload" id="first_com" style="display: block;background:#fff;"><span class="page-reload">还没有评论,快来抢沙发吧!</span></div>');
}
},
getDom:function(){ //获取dom
this.$submit_con=$('#art_scroe_sub'); //提交
this.$text_con=$('#art_textarea_text'); //文本框
this.$limit_con=$('#art_limit'); //字数
this.$uesr_img=$('#uesr_img'); //字数
this.$comment_con=$('#comment_list'); //评论列表
this.$comment_all=$('#comment_all'); //所有评论列表
this.$page_unload=$('#page_unload'); //点击加载
this.$page_load=$('#page_load'); //正在加载
this.limit_min=3; //最小限制字数
this.limit_max=240; //最大限制字数
this.news_id=$("#atr_content").attr("news_id"); //被评论文章的id
this.param=JSON.parse(this.$submit_con.attr('data-id')); //用户评论相关参数
this.comment_limit_time= 10* 1000; //评论限制时间 6s=6*1000 ms
this.like_limit_time= 24 * 60 * 60 * 1000; //点赞限制时间 24h=10*60*1000 ms
this.first=false;
this.$tag = $("#fast_tag");
this.statsUrl = $("#statsParam").attr("data-url"); //统计url
this.aTag = [];

this.$score = $("#get_score");
this.star_score = $("#get_score .up").length;

},
light_star:function(num){ //我想要调用的函数
var $stars = this.$score.children();
if(num>0){
for(var i=0;i<$stars.length;i++){
if(i<num){
$stars.eq(i).addClass('up').removeClass('down');
}else{
$stars.eq(i).addClass('down').removeClass('up');
}
}
}else{
$stars.addClass('down').removeClass('up');
}
},
........
...全文
116 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
redlz2500 2017-02-08
  • 打赏
  • 举报
回复
这个在闭包里面,外面调用不到的 粗暴的方式这样写: var tool,app; (function(){ tool={}; app={}; }) app.light_star(x);
天际的海浪 2017-02-07
  • 打赏
  • 举报
回复
app对象是在(function(){ })()闭包内的,就只能在这个(function(){ })()闭包中调用app.light_star(4); 在(function(){ })()闭包外调用不到,除非把app对象传到(function(){ })()闭包外面。

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧