[英]JSON like Javascript Structure
我敢肯定有人曾经问过这些事情,但是我严重找不到该线程。
我已经写了类似下面的内容,但是我不知道javascript中这种结构的名称,让我们假设其名称像(JSON Structure中的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 () { }
}
};
我不知道您是否在询问模块模式,但是模块模式如下所示:
//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 Module.privilegedMethod();
模块的内部特权方法可以访问其私有变量和私有方法。 因为,它们属于其静态范围。 如果我们有不想公开的数据或方法,可以将它们放在私有方法中。
有关完整的详细信息,请阅读http://www.codeproject.com/Articles/247241/Javascript-Module-Pattern
1)真实名称是什么?
这是一个对象,只是一个普通的旧对象。
2)如何在此结构的parent.parent级别上获取变量,例如在SentenceBuilder.Section.get函数中,我需要获取SentenceBuilder.get函数,我已经尝试过this.this.get,但它无法正常工作。
您不能像这样浏览“上”对象。 要访问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
这是一种对象文学
它是“文字的”,因为它的公共接口正是您键入的内容(加上所有对象/函数/字符串所继承的内容)。
var bob = { name : "Bob", age : 32, getName : function () { return this.name; }, getAge : function () { return bob.age; } };
我不确定您的意思。 但是,除非手动设置,否则JS对象无法获取其父对象。
这样做的简单原因是因为您可以执行此操作
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
对象文字上没有私有属性/方法之类的东西。
如果需要私有属性,则应使用一个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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.