开发者

extend properties in javascript

开发者 https://www.devze.com 2023-01-20 11:51 出处:网络
i have this code in javascript: var object = { get: function(id){ sel = document.getElementById(id); sel.orig = {};

i have this code in javascript:

var object = {
    get: function(id){
        sel = document.getElementById(id);
        sel.orig = {};

        ...

        return object.extend(sel, object);
    }

    extend: function(el, opt){
        for(var name in opt) el[name] = opt[name];
        return el;
    }
}

and in another js i have

var Multi = {
    set: function(){
        if(!this.orig["height"]) this.orig["height"] = this.offsetHeight;

        ...

        return this;
    }
}

object.extend(object,Multi);

and i call it like this:

object.get('myId').set(开发者_如何学Python);

but when in the "set" method, the property this.orig["height"] is always undefined, so it always will change the value and that's not the idea, i need to capture it the first time because im trying to make an Fx framework and i that's for the slideUp function, i need to keep the original height, so i can go back again.

Any ideas please? thank you


Now, in contrast to some people's answers comments being completely non constructive I assume your true question regards this little ditty:

  extend: function(el, opt){
        for(var name in opt) el[name] = opt[name];
        return el;
  }

and why it returns undefined? It does'nt... your problem lies elsewhere, because this works:

var object = {
    get: function(id) {
        el = document.getElementById(id);
        el.orig = {};
        return object.extend(el,object);
    },
    extend: function( el, opt ) {
        for(var name in opt) el[name] = opt[name];
        return el;
    }   
}

var Multi = {
    set: function() {
        if(!this.orig['height']) this.orig['height'] = this.offsetHeight;
        console.log( this.orig['height'] ); // if the value of offsetHeight itself is not undefined, it is what is returned.
    }
}
object.extend(object,Multi);
object.get('myId').set();


hey, thanks for the comments and overall for the answer Quickredfox!! i found the problem, in this part:

 get: function(id) {
    el = document.getElementById(id);
    el.orig = {};
    return object.extend(el,object);
},

i just changed to this:

 get: function(id) {
    el = document.getElementById(id);
    if(!el.orig) el.orig = {};
    return object.extend(el,object);
},

and voila!! thank you very much for your answer!

0

精彩评论

暂无评论...
验证码 换一张
取 消