简体   繁体   English

在类范围内定义javascript的变量

[英]defining the variables of javascript inside the class scope

guys i know it is dummy question but i tried alot and never reached .. i want to define the variables of Javascript file inside the scope of the class not to be overridden by any outside source .. here is my code 伙计们,我知道这是一个虚拟问题,但是我尝试了很多,但从未尝试过。.我想在类范围内定义Javascript文件的变量,以免被任何外部源覆盖..这是我的代码

 var pageSize = 5; var startIndex = 0; var endIndex = startIndex + pageSize; var page = 1; var textField; var check = 0; // i want all of the above variables to be defined inside the scope of the class not outside function bindContext(fn, context) { return function() { fn.apply(context, arguments); } } function GridLibraryDep(data) { this.data = data; this.columns = $.map(this.data[0], function(item, key) { return key; }); document.getElementById("from").innerHTML = 1; document.getElementById("to").innerHTML = pageSize; document.getElementById("total").innerHTML = data.length; $("#first").click(bindContext(this.first, this)); $("#last").click(bindContext(this.last, this)); } GridLibraryDep.prototype = { first : function() { var size = this.data.length; page = 1; // document.getElementById("lbl").innerHTML = page; endIndex = page * pageSize; startIndex = endIndex - pageSize; this.deleteTable(); document.getElementById("from").innerHTML = 1; document.getElementById("to").innerHTML = endIndex; document.getElementById("total").innerHTML = size; this.display(); }, last : function() { var size = this.data.length; endIndex = size; startIndex = Math.floor(size / pageSize) * pageSize; page = Math.ceil(size / pageSize); this.deleteTable(); // document.getElementById("lbl").innerHTML = page; document.getElementById("from").innerHTML = startIndex + 1; document.getElementById("to").innerHTML = endIndex; document.getElementById("total").innerHTML = size; this.display(); }}; 

This is a common way to create a prototype Class - https://jsfiddle.net/aaronfranco/fysbbdp7/10/ 这是创建原型类的常见方法-https: //jsfiddle.net/aaronfranco/fysbbdp7/10/

var MyClass = MyClass || {};

(function () {
    "use strict";

    //============================================================
    // Constructor - MUST BE AT TOP OF FILE
    //------------------------------------------------------------
    MyClass = function () {
        this.created = true;
    };

    //============================================================
    // Member Functions & Variables
    //------------------------------------------------------------
    MyClass.prototype = {
        pageSize:5,
        startIndex:0,
        endIndex:0,
        page:1,
        textField:null,
        check:0,
        init: function(){
            this.endIndex = this.startIndex + this.pageSize;
        },
        first : function() {
          // function here
          console.log(this.endIndex);
        },

        last : function() {
          // function here
          //alert(this.pageIndex);
          console.log(this.pageSize)
        },
        iterater: function(int) {
          console.log(this.pageSize);
          this.pageSize += int;
        }
    }
})();

Here is the example usage available in the JSFiddle link above: 这是上面的JSFiddle链接中可用的示例用法:

var newClass = new MyClass();
newClass.init();
newClass.first();

for(var i=0; i<10; i++){
    newClass.iterater(i);
  newClass.last();
}

Using this "namespaced" technique, you can instantiate this class using 使用这种“命名空间”技术,您可以使用实例化此类

var newClass = new MyClass();
newClass.init();

You could then create protected variables in the class by making them private. 然后,您可以通过将它们设为私有来在类中创建受保护的变量。 There is a good article about some potential ways to create protected variables here. 这里有一篇很好的文章介绍了一些创建受保护变量的潜在方法。

http://philipwalton.com/articles/implementing-private-and-protected-members-in-javascript/ http://philipwalton.com/articles/implementing-private-and-protected-members-in-javascript/

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

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