JSON就像Javascript结构一样

时间:2014-01-30 12:00:35

标签: javascript jquery json

我相信有人之前已经问过这些事情,但我真的无法找到那个帖子。

我写了类似下面的内容,但我不知道javascript中这种结构的名称,我们假设它的名字就像(JSON结构中的Javascript)。

我需要知道这种结构的数量, 1)它的实际名称是什么? 2)如何在这个结构中获取parent.parent级别的变量,就像在SentenceBuilder.Section.get函数中我需要获得SentenceBuilder.get函数,我已经尝试过this.this.get但它无法正常工作。 3)在此结构中声明私有变量的最佳方法是什么,只能在其函数中使用。我试过_sentenceBuilder:new Object(),可以吗?

提前非常感谢你......

var SentenceBuilder = {
    _sentenceBuilder: new Object(),
    _section: new Object(),
    _group: new Object(),
    _question: new Object(),

    get: function () {
        return this._sentenceBuilder;  //for speeding up;  --wasim
        //return store.fetchSession("SENTENCE_BUILDER_" + VisitID);        
    },
    set: function () {
        store.saveSession("SENTENCE_BUILDER_" + VisitID, data);
        this._sentenceBuilder = $(data);
    },
    Section: {
        get: function () {

            this.this._section = this.this.get().find("SECTION[SECTION_SEQ_NUM='" + sectionID + "']");
            return this.this._section;
        },
        set: function () { },
    },
    Group: {
        get: function () {
            this.this._group = this.this.Section.get().find("GROUP[QUESTION_GROUP_ID='" + groupID + "']");
            if (this.this._group.length == 0) {
                this.this._question = this.this.Section.get().find("QUESTION[QUESTION_ID='" + questionID + "']");
                this.this._group = this.this._question.parent();
            }
            return this.this._group;
        },
        set: function () { }

    },
    Question: {
        get: function () {
            this.this._question = this.this._group.find("QUESTION[QUESTION_ID='" + qId + "']");
            return this._question;
        },
        set: function () { }

    }
};

3 个答案:

答案 0 :(得分:1)

我不知道您是否询问模块模式,但模块模式如下所示:

//Single Global Variable "Module"
var Module = ( function ( ) {
 var privateVariable = "some value",
    privateMethod = function ( ) {
        //do something.
 };
//returning one anonymous object literal that would expose privileged methods.
 return {
     //the method inside the return object are 
     //called as privileged method because it has access 
     //to the private methods and variables of the module.

     privilegedMethod : function ( ) {
        //this method can access its private variable and method 
        //by using the principle of closure. 
        alert(privateVariable); //accessing private variable.
        privateMethod( ); //calling private method
    }
 };
})( );

此处Module是展示给文档的单一全局变量。我们声明,调用一个匿名方法并将其分配给Module变量。

现在我们可以通过编写Module.privilegedMethod();内部特权来调用privilegedMethod模块的方法可以访问它的私有变量和私有方法。因为,它们属于静态范围。如果我们有任何我们不想公开的数据或方法,我们可以将它们放在私有方法中。

有关完整详情,请参阅http://www.codeproject.com/Articles/247241/Javascript-Module-Pattern

答案 1 :(得分:1)

  

1)它的实际名称是什么?

这是一个物体,只是一个普通的旧物体。

  

2)如何在此结构中获取parent.parent级别的变量,就像在SentenceBuilder.Section.get函数中我需要获得SentenceBuilder.get函数,我已经尝试过this.this.get但它不起作用。< / p>

您无法像这样导航“向上”对象。要访问SentenceBuilder.Section.get,您必须输入SentenceBuilder.Section.get,或者开始手动存储parent变量,以便每个对象都有对其父级的引用

  

3)在此结构中声明私有变量的最佳方法是什么,只能在其函数中使用。我试过_sentenceBuilder:new Object(),可以吗?

是的,这很常见,虽然它仅由约定私有。如果你想要真正的私有变量,你需要使用闭包。将您的对象放在IIFE中并将其返回:

var SentenceBuilder = (function () {

  var sentenceBuilder: new Object(),
  var section: new Object(),
  var group: new Object(),
  var question: new Object(),

  var SentenceBuilder = {
    // above variables can be accessed here...
    // ...
  };

  return SentenceBuilder;
})();

// ... but not here or anywhere else

答案 2 :(得分:1)

  1. 这是一个Object-Literal
    它是“文字”,因为它的公共接口正是你输入的内容(加上所有对象/函数/字符串继承的东西)。

    var bob = {
        name : "Bob",
        age  : 32,
        getName : function () { return this.name; },
        getAge  : function () { return bob.age;   }
    };
    
  2. 我不完全确定你的意思。但是JS对象有 no 获取父对象的方式,除非你手动设置它 原因很简单,因为你可以这样做

    var stewie = {
        name : "Stewie",
        size : "small"
    },
    
    peter = {
        name : "Peter",
        size : "large",
        child : stewie
    },
    
    lois = {
        name : "Lois",
        size : "average",
        child : stewie
    };
    
    peter.child === lois.child; // true
    
    stewie.parent; // what would this be?
    
    //manually store a reference to parent
     var parent_object = {
            data : "",
            child_object : {
                data : "",
                parent : parent_object
            }
        };
    
     parent_object.child_object.parent === parent_object;  //true
    
  3. Object-Literal上没有私有属性/方法 如果你想要一个私有属性,那么你应该使用function(不一定是构造函数),并从中返回一个公共接口。

    var my_object = (function () {
        var my_secret = 42, // private
            data = { },     // private
            validate = function (guess) { return guess === my_secret; }, // private
    
            guess_secret = function (guess) {
                return validate(guess) ? data : null;
            },
    
            public_interface = {
                secret : {
                    owner : public_interface,
                    guess : guess_secret
                }
            };
    
        return public_interface;
    }());
    
    
    my_object.my_secret;  // undefined
    my_object.validate(); // error
    
    var secret_data = my_object.secret.guess(42);