在 JavaScript 中扩展属性

发布于 2024-09-27 05:01:01 字数 771 浏览 0 评论 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 功能的,我需要保持原始高度,这样我可以再次返回。

有什么想法吗?谢谢

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪少女 2024-10-04 05:01:01

现在,与一些人的 answers 评论完全没有建设性相反,我认为你真正的问题是关于这个小小曲:

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

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();
冷心人i 2024-10-04 05:01:01

嘿,感谢您的评论和 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);
},

瞧!非常感谢您的回答!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文