jquery使用嵌套遍历_使用jQuery创建和检索嵌套对象

jquery使用嵌套遍历

The ability to create and retrieve nested objects by objectified string path is incredibly useful.  Not only do you not need to do the manual string of object/property checks to avoid "{x} is not defined" errors, but if you create your method properly, you can also set an empty object in its given place (or namespace, some would call it.    Here's how you can add this functionality to the jQuery object.

通过对象化字符串路径创建和检索嵌套对象的功能非常有用。 您不仅不需要手动进行对象/属性检查的字符串来避免出现“未定义{x}”错误,而且如果您正确地创建方法,还可以在其给定位置(或名称空间)设置一个空对象,有些人会称之为。这是将功能添加到jQuery对象的方法。

jQuery JavaScript (The jQuery JavaScript)

Since jQuery's philosophy is to use the same method for getting and setting, we'll be doing this same for creating and retrieving objects;  in this case, we'll use the obj method:

由于jQuery的理念是使用相同的方法进行获取和设置,因此我们将在创建和检索对象时采用相同的方法。 在这种情况下,我们将使用obj方法:


(function() {

	// Utility method to get and set objects that may or may not exist
	var objectifier = function(splits, create, context) {
		var result = context || window;
		for(var i = 0, s; result && (s = splits[i]); i++) {
			result = (s in result ? result[s] : (create ? result[s] = {} : undefined));
		}
		return result;
	};

	// Gets or sets an object
	jQuery.obj = function(name, value, create, context) {
		// Setter
		if(value != undefined) {
			var splits = name.split("."), s = splits.pop(), result = objectifier(splits, true, context);
			return result && s ? (result[s] = value) : undefined;
		}
		// Getter
		else {
			return objectifier(name.split("."), create, context);
		}
	};

})();


As with the MooTools alternative, the objectifier function is enough to handle both getting and setting, as well as doing both within a given context (existing object).  Here are a few examples of how you can use the new jQuery.obj method:

MooTools替代方法一样 ,对象objectifier功能足以处理获取和设置,以及在给定上下文(现有对象)中进行这两种操作。 以下是一些有关如何使用新的jQuery.obj方法的示例:


// Check for existence
var moduleNameExists = jQuery.obj("mynamespace.widget.moduleName"); // undefined

// Create the obj
jQuery.obj("mynamespace.widget.moduleName", { prop: 1 }); // mynamespace.widget.moduleName.prop returns 1

// Create an object on existing object
jQuery.obj("subnamespace.subModuleName", { someProp: 8 }, true, mynamespace.widget);
	// mynamespace.widget.subnamespace.subModuleName = { someProp: 8 }


As I work more with jQuery, both with its provided methods and other third party tools, accessing arbitrary objects by string and sometimes context allows me to avoid the dance of manual object and property existence checks.  Of course the essence of this script is really the objectifier method, which can be added to any framework or toolkit, but with a framework as popular as jQuery, why not just put it out there for everyone?

当我更多地使用jQuery时,无论是使用其提供的方法还是使用其他第三方工具,都可以通过字符串访问任意对象,有时还可以通过上下文访问,从而避免了手动对象和属性存在检查的麻烦。 当然,该脚本的本质实际上是objectifier方法,可以将其添加到任何框架或工具箱中,但是具有与jQuery一样流行的框架,为什么不将其提供给所有人呢?

翻译自: https://davidwalsh.name/jquery-objects

jquery使用嵌套遍历

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值