首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有方法迭代javascript对象的原型变量和函数?

有没有方法迭代javascript对象的原型变量和函数?
EN

Stack Overflow用户
提问于 2012-10-29 05:28:44
回答 1查看 1.8K关注 0票数 2

这个问题不再适用于我想要做的事情,因为我必须用名称.声明pmp对象的名称。

我希望能够将对象与类的每个prototype函数关联起来,而不需要对每个原型手动执行。我试着用下面的方法来查看原型中的内容,但是没有结果

代码语言:javascript
复制
myclass.prototype.length == undefined;
myclass.prototype.toString() == [object Object];
myclass.prototype == [object Object];

这是我的密码。在以下几行:

代码语言:javascript
复制
this.appear = _pmp(k8rModal.prototype._appear);
this.centerSlate = _pmp(k8rModal.prototype._centerSlate);
this.adjustToScreenResize = _pmp(k8rModal.prototype._adjustToScreenResize);

我运行一个名为“_pmp”的函数,该函数创建一个出现在centerSlate & adjustToScreenResize中的PreMainPost对象。这些对象具有一个.run()函数,它首先运行pre()函数,然后运行main()函数,构造函数参数定义这个函数,最后运行post()函数。

这是所有的上下文: k8r-modal.js

代码语言:javascript
复制
function k8rModal(DOMnamespace){
var _ = this._ = DOMnamespace+"_"; // for DOM namespacing

this.tightWrap=1;
this.visible = 0;

$('body').prepend('<div id="'+_+'stage"></div>');
this.stage = stage = $('#'+_+'stage');
stage.css({
    'display':'none',
    'width':'100%',
    'height':'100%',
    'position': 'fixed',
    'left':'0px',
    'top':'0px',
    'opacity': '.6',
    'background-color':'#333'
});

$('body').append('<div id="'+_+'slate"></div>');
this.slate = slate = $('#'+_+'slate');
slate.css({
    'display':'none',
    'width':'640px',
    'height':'480px',
    'position': 'fixed',
    'left':'0px',
    'top':'0px',
    'background-color':'#eee'
});

var k8rModalInstance = this;
$('.'+_+'caller').on('click',function(){
    k8rModalInstance.appear.run();
});

this.appear = _pmp(k8rModal.prototype._appear);
this.centerSlate = _pmp(k8rModal.prototype._centerSlate);
this.adjustToScreenResize = _pmp(k8rModal.prototype._adjustToScreenResize);

this.centerSlate.run();
this.word="asdf";
$(window).resize(function(){
    alert(k8rModalInstance.word)
  k8rModalInstance.adjustToScreenResize.run();
});
}

k8rModal.prototype._appear = function(){
this.that.visible = 1;
this.that.slate.show();
this.that.stage.show();
}

k8rModal.prototype._centerSlate = function(){
var wWidth, wHeight, slate = $(this.that.slate) ;

wWidth = $(window).width();
wHeight = $(window).height();

slate.css({
    top: (wHeight/2 - ( slate.height()/2))+"px",
    left: ( wWidth/2 - ( slate.width()/2 ) )+"px"
});
}

k8rModal.prototype._adjustToScreenResize = function(){
this.that.centerSlate.run();

}

(pre post.js) pmp.js:

代码语言:javascript
复制
function _pmp(func){
return new pmp(this,func);
}

function pmp(that,func){
var func;
this.pre = new funcList();
this.post = new funcList();
this.main = func;
this.that = that;
}

pmp.prototype.run = function(arg){
this.pre.run(arg);
this.post.run(arg,this.main());
}

pmp.prototype.trim = function(){
this.pre = new funcList();
this.post = new funcList();
}

(包含函数列表的对象)funcList.js:

代码语言:javascript
复制
function funcList(){
this.unique; // if a function can be 
this.list=[];
}

funcList.prototype.add = function(func){
if (this.unique){
    var passing = 1;
    for(var i; i<this.list.length; i+=1){
        if (list[i].toString == func.toString){
            passing = 0;
        }
    }
    if (passing){
        this.list.push(func);   
    }
}else{
    this.list.push(func);
}
}

funcList.prototype.remove = function(func){
for(var i; i<this.list.length; i+=1){
    if (list[i].toString == func.toString){
        this.list.splice(i,1);
    }
}
}

funcList.prototype.clear = function(){
this.list = [];
}

funcList.prototype.run = function(arg){
for(var i; i<this.list.length; i+=1){
    (this.list[i])(arg);
}
}
EN

回答 1

Stack Overflow用户

发布于 2012-10-29 05:34:27

只有土著人和函数的原型才能为您所访问。如果myClass是一个函数,则可以使用

代码语言:javascript
复制
for(var prop in myClass.prototype){
    console.log(myClass.prototype[prop]);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13116308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档