is javascript prototype with closure a good thing in terms of good practice

I'm trying to learn javascript for a few weeks (node.js). I have some provious knowledge of .NET, and that turns out to be a bad thing. My understanding of javascript in general is that:

  • everything should be put in functions, we can think of them as classes, but not call them that way
  • functions (methods) should always be put in a prototype, a prototype is a something like a shared function for every instance of our "class", that is - instatiated once for all of them
  • closures are some good thing, they hide private methods, they also seem to last in memory until unknown moment, holding vars
  • That was written just so you get my current understanding of JS, but the actual question is below: I have some code, hopefully a closure in a prototype, and thus - written in my weak undestanding close to how javascript was intended to be used. So,

  • is javascript prototype with closure a good thing in terms of
    good practice, if it is, is this code good example of that?
  • why whould I want to have "return this" in myclass constructor?
  • and finaly - are parts of code no1 and no2 the same thing?
  • function createFunctionWithClosure() { // number, and a gensym method that produces unique strings
        var prefix = '';
        var seq = 0;
        return {
          set_prefix(p) {
              prefix = String(p);
            },
            set_seq: function(s) {
              seq = s;
            },
            get_unique: function() {
              var result = prefix + seq;
              seq += 1;
              return result;
            }
        };
      }
    
    //example no1
    function myclass_t() {
      //why some examples have
      return this
    }
    myclass_t.prototype = createFunctionWithClosure();
    var myclass = new myclass_t()
    myclass.set_prefix('no1--->')
    myclass.set_seq(100)
    console.log(myclass.get_unique()) //100
    console.log(myclass.get_unique()) //101...
    
    //example no2
    var myvar = createFunctionWithClosure();
    myvar.set_prefix('no2-->')
    myvar.set_seq(1000);
    myvar.get_unique()
    myvar.get_unique()
    console.log(myvar.get_unique()) //1002
    console.log(myvar.get_unique()) //1003

    This is not a direct answer to your question, but I think that what you actually need is to understand the basics of how the prototypal inheritance works in javascript.

    Here is a good video and a live playground and Mozilla docs is also a good source of information.

    Once you understand these things, you may feel like it is a "hackish" way to do things and you whether will need to accept this or move to ES6 or some compile-to-javascript language like TypeScript, see this question for more information about this.


    #1 is javascript prototype with closure a good thing in terms of good practice, if it is, is this code good example of that?

    A closure is a function where its own scope is referencing members from other scopes:

    // "x" is being referenced on the inline function's scope
    var x = "hello world";
    var func = function() {
       console.log(x);
    };
    

    Whether is a good idea or not, it'll depend on project's requirements... I would say that there're few cases where I would go this route.

    #2 why whould I want to have "return this" in myclass constructor?

    Because of some corner case. If you're developing a fluent interface you might want to return this , and you would do it if the whole function isn't being called from the object where it was declared.

    For example:

    var obj = {
        doStuff: function() {
           return this;
        }
    
        doStuff2: function() {
           return this;
        }
    };
    
    // This is a fluent interface
    obj.doStuff().doStuff2();
    

    In a constructor function returning this is useless unless you want to call the whole constructor function as a regular function too:

    var func = function() {
        return this;
    };
    
    // The whole "return this" won't have any effect
    // on this case
    var instance = new func();
    
    // With Function.prototype.call, the first parameter
    // defines "this" within the scope of the called function.
    // That is, it'll return the object with a text property...
    var result = func.call({ text: "hello world" });
    

    #3 and finaly - are parts of code no1 and no2 the same thing?

    No.

    Your first sample shows a constructor function where its prototype is the object returned by the factory function.

    In the other hand, your second sample uses the whole factory function to set its returned object to the whole variable.

    链接地址: http://www.djcxy.com/p/60776.html

    上一篇: 在Excel中从公式返回空单元格

    下一篇: 是关于JavaScript的原型封闭好的做法方面的一件好事