Javascript 如何使用jquerymx在类中使用公共方法
我有一个关于jquerymx的问题。 我想公开deque类中的某个方法。 因为它将用于其他类。 示例)我认为pushback是deque类中的私有方法。所以我想把它改成公共的。 我怎样才能解决这个问题Javascript 如何使用jquerymx在类中使用公共方法,javascript,jquery,Javascript,Jquery,我有一个关于jquerymx的问题。 我想公开deque类中的某个方法。 因为它将用于其他类。 示例)我认为pushback是deque类中的私有方法。所以我想把它改成公共的。 我怎样才能解决这个问题 deque : function(){ this.elements = []; // pushback :function(element) // { // this.elements.push(element); // } this.p
deque : function(){
this.elements = [];
// pushback :function(element)
// {
// this.elements.push(element);
// }
this.push_back = function(element){
this.elements.push(element);
}
this.push_front = function(element){
this.elements.unshift(element);
}
this.pop_back = function(){
return this.elements.pop();
}
this.pop_front = function() {
return this.elements.shift();
}
this.peek_back = function(){
return this.elements[this.elements.length-1];
}
this.peek_front = function(){
return this.elements[0];
}
this.isEmpty = function(){
return this.elements.length === 0;
}
this.getElements = function(number){
return this.elements[number];
}
this.Length = function(){
return this.elements.length;
}
}
谢谢你的回复
编辑:
Widget.js
(function($,S){
S.Claass('Widget',{
init:function(){
},
deque : function(){
this.elements = [];
push_back = (function(element){
this.elements.push(element);
}).bind(this);
push_front = (function(element){
this.elements.unshift(element);
}).bind(this);
pop_back = (function(){
return this.elements.pop();
}).bind(this);
pop_front = (function() {
return this.elements.shift();
}).bind(this);
peek_back = (function(){
return this.elements[this.elements.length-1];
}).bind(this);
peek_front = (function(){
return this.elements[0];
}).bind(this);
isEmpty = (function(){
return this.elements.length === 0;
}).bind(this);
getElements = (function(number){
return this.elements[number];
}).bind(this);
Length = (function(){
return this.elements.length;
}).bind(this);
});
})(jQuery, ...);
------.js
(函数($,S){
S.Widget('----'{
init:function(){
this.tempDeque=新this.deque();
},
设置:函数(){
....
对于(变量i=0;i<10;i++)
{
.....
this.tempDeque.push_back(i);///这是错误。
}
}
});
})(jQuery,…);
pushback=(函数(元素){this.elements.push(元素);}).bind(this)调用deque
函数后,code>使其在全局范围内可用。不确定这是你要找的。但是你可以返回这个代码>从deque函数中,然后访问所有相关方法。console.log是“无法调用未定义的方法‘push_back’”。这是错误的。push_back=(函数(元素){this.elements.push(元素);}).bind(this);我写这个。参见编辑行。我改为你的建议,但返回错误。
(function($, S){
S.Widget('-----',{
init:function(){
this.tempDeque = new this.deque();
},
setup:function(){
....
for(var i = 0; i < 10; i++)
{
.....
this.tempDeque.push_back(i); /// this is error.
}
}
});
})(jQuery, ...);