0

我在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;
    }
}

在另一个js中我有

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

        ...

        return this;
    }
}

object.extend(object,Multi);

我这样称呼它:

object.get('myId').set();

但是在“set”方法中,属性 this.orig["height"] 总是未定义,所以它总是会改变值,这不是想法,我需要第一次捕获它,因为我试图制作一个Fx 框架和我那是为 slideUp 功能,我需要保持原来的高度,所以我可以再回去。

请问有什么想法吗?谢谢你

4

2 回答 2

0

现在,与某些人的回答完全不具建设性相比,我认为您的真正问题是关于这个小曲子:

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

为什么它返回未定义?它没有......你的问题出在其他地方,因为这有效:

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();
于 2010-10-13T03:42:17.997 回答
0

嘿,感谢您的评论和整体的回答 Quickredfox!我在这部分发现了问题:

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

我刚改成这样:

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

瞧!非常感谢您的回答!

于 2010-10-15T13:11:55.213 回答