类似JSON的Javascript结构
我肯定以前有人问过这些问题,但我真的找不到线索 我写了下面这样的东西,但我不知道这种结构在javascript中的名称,让我们假设它的名称是(JSON结构中的javascript) 我需要知道这种结构的一些东西, 1) 如果有的话,它的实际名称是什么? 2) 如何在此结构中获取parent.parent级别的变量,如在SentenceBuilder.Section.get函数中我需要获取SentenceBuilder.get函数,我尝试了this.this.get,但它不起作用。 3) 在这个只能在函数中使用的结构中声明私有变量的最佳方法是什么。我试过了_sentenceBuilder:new Object(),可以吗 提前非常感谢你类似JSON的Javascript结构,javascript,jquery,json,Javascript,Jquery,Json,我肯定以前有人问过这些问题,但我真的找不到线索 我写了下面这样的东西,但我不知道这种结构在javascript中的名称,让我们假设它的名称是(JSON结构中的javascript) 我需要知道这种结构的一些东西, 1) 如果有的话,它的实际名称是什么? 2) 如何在此结构中获取parent.parent级别的变量,如在SentenceBuilder.Section.get函数中我需要获取SentenceBuilder.get函数,我尝试了this.this.get,但它不起作用。 3) 在这个只
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 () { }
}
};
我不知道您是否询问模块模式,但模块模式如下所示:
//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.privilegedMethod()调用privilegedMethod代码>模块的内部特权方法可以访问其私有变量和私有方法。因为,它们属于静态范围。如果我们有任何不想公开的数据或方法,我们可以将它们放在私有方法中
有关完整的详细信息,请阅读我不知道您是否询问模块模式,但模块模式如下所示:
//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.privilegedMethod()调用privilegedMethod代码>模块的内部特权方法可以访问其私有变量和私有方法。因为,它们属于静态范围。如果我们有任何不想公开的数据或方法,我们可以将它们放在私有方法中
有关完整的详细信息,请阅读我不知道您是否询问模块模式,但模块模式如下所示:
//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.privilegedMethod()调用privilegedMethod代码>模块的内部特权方法可以访问其私有变量和私有方法。因为,它们属于静态范围。如果我们有任何不想公开的数据或方法,我们可以将它们放在私有方法中
有关完整的详细信息,请阅读我不知道您是否询问模块模式,但模块模式如下所示:
//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.privilegedMethod()调用privilegedMethod代码>模块的内部特权方法可以访问其私有变量和私有方法。因为,它们属于静态范围。如果我们有任何不想公开的数据或方法,我们可以将它们放在私有方法中
有关完整的详细信息,请阅读
1) 如果有的话,它的实际名称是什么
这是一个物体,只是一个普通的旧物体
2) 如何在此结构中获取parent.parent级别的变量,如在SentenceBuilder.Section.get函数中我需要获取SentenceBuilder.get函数,我尝试了this.this.get,但它不起作用
你不能像那样“向上”导航对象。要访问SentenceBuilder.Section.get
,您必须直接键入SentenceBuilder.Section.get
,或者开始手动存储父变量
,以便每个对象都有对其父对象的引用
3) 在这个只能在函数中使用的结构中声明私有变量的最佳方法是什么。我试过了_sentenceBuilder:new Object(),可以吗
是的,这是很常见的,尽管这只是惯例上的隐私。如果想要真正的私有变量,就需要使用闭包。将您的对象放入并返回:
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
1) 如果有的话,它的实际名称是什么
这是一个物体,只是一个普通的旧物体
2) 如何在此结构中获取parent.parent级别的变量,如在SentenceBuilder.Section.get函数中我需要获取SentenceBuilder.get函数,我尝试了this.this.get,但它不起作用
你不能像那样“向上”导航对象。要访问SentenceBuilder.Section.get
,您必须直接键入SentenceBuilder.Section.get
,或者开始手动存储父变量
,以便每个对象都有对其父对象的引用
3) 在这个只能在函数中使用的结构中声明私有变量的最佳方法是什么。我试过了_sentenceBuilder:new Object(),可以吗
是的,这是很常见的,尽管这只是惯例上的隐私。如果想要真正的私有变量,就需要使用闭包。将您的对象放入并返回:
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
1) 如果有的话,它的实际名称是什么
这是一个物体,只是一个普通的旧物体
2) 如何在此结构中获取parent.parent级别的变量,如在SentenceBuilder.Section.get函数中我需要获取SentenceBuilder.get函数,我尝试了this.this.get,但它不起作用
你不能像那样“向上”导航对象。要访问SentenceBuilder.Section.get
,您必须直接键入SentenceBuilder.Section.get
,或者开始手动存储父变量
,以便每个对象都有对其父对象的引用
3) 在这个结构中声明私有变量的最好方法是什么