3

This question is no longer applicable to what I'm trying to do because I have to declare the name of the pmp objects with a name.

I want to be able to associate an object with each prototype function for my class without doing it manually for each prototype. I've tried seeing what's inside prototype with the following methods, but to no avail

myclass.prototype.length == undefined;
myclass.prototype.toString() == [object Object];
myclass.prototype == [object Object];

This is my code. In the following lines:

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

I run a function called '_pmp' that creates a PreMainPost object that appear,centerSlate & adjustToScreenResize will refer to. These objects have a .run() function that will first run the pre() functions then the main() function which is being defined by the constructor parameter and then finally the post() functions.

This is all the context: k8r-modal.js

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-main-post.js) pmp.js:

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();
}

(an object that contains a list of functions)funcList.js:

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);
}
}
4
  • 2
    Are you able to clarify what it is you're trying to do? Commented Oct 29, 2012 at 5:30
  • 3
    show the code "you doing it manually for each prototype". Commented Oct 29, 2012 at 5:32
  • Does this help?: jsfiddle.net/gQUn3/1 Commented Oct 29, 2012 at 5:38
  • I suppose the question is no longer relevant because I still have to declare the name of the variables that I assign the functions to (which are really objects that contains lists of functions). Commented Oct 29, 2012 at 14:24

1 Answer 1

2

Only the prototypes for natives and functions are accessible to you. If myClass is a function, you can iterate over its prototype by using

for(var prop in myClass.prototype){
    console.log(myClass.prototype[prop]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by "only the prototypes for natives and functions are accessible"? Which prototype objects are "not accessible"?
@Bergi The internal [[Prototype]] property of an object declared like this: var x = {}; is not accessible except through the use of Object.getPrototypeOf(x). Perhaps I should have said directly accessible?
OK, "not accessible as a direct property" - unless you count non-standard __proto__ :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.