简体   繁体   English

将window.onresize合并到OO JS类中

[英]Incorporate window.onresize into OO JS class

I'm just trying to structure my Javascript better and wondering how to incorporate window.onresize into the returned object, like so: 我只是想更好地构造Javascript,想知道如何将window.onresize合并到返回的对象中,如下所示:

var baseline = function(){

    var tall, newHeight, target, imgl, cur, images = [];

    return {

        init: function(selector, target){
            this.images = document.querySelectorAll(selector);
            this.target = target;
            this.setbase(this.images);
            window.onresize = this.setbase(this.images);
        },

        setbase: function(imgs){
            this.imgl = imgs.length;
            if(this.imgl !== 0){
                while(this.imgl--){
                    this.cur = imgs[this.imgl];
                    this.cur.removeAttribute("style");
                    this.tall = this.cur.offsetHeight;
                    this.newHeight = Math.floor(this.tall / this.target) * this.target;
                    this.cur.style.maxHeight = this.newHeight + 'px';
                }
            } else {
                return false;
            }
        }

    }

}();

Is this the way that people would do it, is this going to work? 这是人们会这样做的方式,这会起作用吗? Thanks 谢谢

EDIT: 编辑:

Invoked like so: 像这样调用:

window.onload = function(){
        baseline.init('img', '24');
    };

I would like it so that when the window is resized, baseline.init is called with the same params as the initial init function call... 我希望这样,以便在调整窗口大小时,使用与初始init函数调用相同的参数来调用baseline.init。

Here's the main error 这是主要错误

init: function(selector, target){
    this.images = document.querySelectorAll(selector);
    this.target = target;
    this.setbase(this.images);
    // This line says call setbase now and assign the result of that
    // as the onresize handler
    window.onresize = this.setbase(this.images);
},
  • Your this.images does not point to the var images = [] you've created. 您的this.images没有指向您创建的var images = [] This is for when you're using protoype style objects. 这是在您使用原型样式对象时使用的。 You should just use images in your functions. 您应该只在函数中使用images
  • Some of your variables look like they're only used in setBase, they should be local 您的某些变量看起来仅在setBase中使用,它们应该是局部的
  • Looking at your object, it's very hard to tell what it's supposed to do, sounds like you're wrapping code in an object just for the sake of wrapping it into an object. 看着您的对象,很难说出它应该做什么,听起来就像您只是为了将代码包装到一个对象中而将代码包装在一个对象中。 What does baseline mean? 基线是什么意思?

Here's a better version of your code, you should read and understand http://www.joezimjs.com/javascript/javascript-closures-and-the-module-pattern/ and http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html so you can decide what pattern you want to use and how they actually work. 这是代码的更好版本,您应该阅读并了解http://www.joezimjs.com/javascript/javascript-closures-and-the-module-pattern/http://js-bits.blogspot.com/ 2010/08 / javascript-inheritance-done-right.html,以便您可以决定要使用哪种模式以及它们的实际工作方式。 You are mixing both patterns, even though you didn't intend to. 即使您不打算混合两种模式,也要混合使用。 The trick is that with the way you're writing it (module pattern) there's no need to use this in the code, they're actually local variables held be the module 诀窍在于,使用您编写代码的方式(模块模式),无需在代码中使用this ,它们实际上是保存在模块中的局部变量

var baseline = function(){
    // Don't use "this.tall", just "tall" gets you the variable
    // Class variables, are you sure you need them throughout the class
    var tall, newHeight, target, imgl, cur, images = [];

    // Different name for the parameter so it doesn't get confused with 
    // the class variables
    function init(selector, pTarget) {
        images = document.querySelectorAll(selector);
        target = pTarget;
        setBase();
        // Since we're not using this, you 
        // can just reference the function itself
        window.onresize = setBase
    }

    // Most JS developers name methods using camelCase
    function setBase() {
        imgl = imgs.length;
        if(imgl !== 0){
            while(imgl--){
                cur = imgs[imgl];
                cur.removeAttribute("style");
                tall = cur.offsetHeight;
                newHeight = Math.floor(tall / target) * target;
                cur.style.maxHeight = newHeight + 'px';
            }
            // should you return true here? what does returning 
            // something even mean here?
        } else {
            return false;
        }
    }

    // Return just the public interface
    return {    
        init: init
        setBase: setBase
    };   
}();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM