使用JavaScript获取和设置嵌套对象

Back when JavaScript frameworks like MooTools and jQuery ruled the land we all wrote tutorials which were framed more toward the given framework instead of vanilla JavaScript.  Sad but true.  These days I avoid framework-oriented posts since Node.js has taken over the world and JavaScript toolkits come and go.

早在MooToolsjQuery之类JavaScript框架占据统治地位时,我们所有人都编写了一些教程,这些教程更多地针对给定的框架,而不是原始JavaScript。 悲伤但真实。 这些天来,我避免使用面向框架的文章,因为Node.js接管了世界,并且JavaScript工具包不断出现。

One very useful post I wrote and still love is Create and Retrieve Nested Objects with MooTools.  In that post I showed you how you can easily get and set nested objects, since doing existence checks down the object chain in a manual way is ... ugly.  Let's tear this functionality out of its MooTools orientation so you can take it with you wherever you go!

我写过但仍然喜欢的一个非常有用的文章是使用MooTools创建和检索嵌套对象 。 在那篇文章中,我向您展示了如何轻松获取和设置嵌套对象,因为以手动方式对存在的对象链进行检查很丑。 让我们从MooTools的方向上撕下此功能,以便随身携带它!

JavaScript (The JavaScript)

We'll use a simple immediately-executing function to wrap the underlying "worker" function and return an object with properties for getting, setting, and checking existence:

我们将使用一个简单的立即执行函数来包装基础的“ worker”函数,并返回一个具有获取,设置和检查存在性的属性的对象:


var Objectifier = (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;
	};

	return {
		// Creates an object if it doesn't already exist
		set: function(name, value, context) {
			var splits = name.split('.'), s = splits.pop(), result = objectifier(splits, true, context);
			return result && s ? (result[s] = value) : undefined;
		},
		get: function(name, create, context) {
			return objectifier(name.split('.'), create, context);
		},
		exists: function(name, context) {
			return this.get(name, false, context) !== undefined;
		}
	};

})();


So how would you use this set of functions?  Here are some sample usage examples:

那么您将如何使用这套功能? 以下是一些示例用法示例:


// Creates my.namespace.MyClass
Objectifier.set('my.namespace.MyClass', {
	name: 'David'
});
// my.namespace.MyClass.name = 'David'

// Creates some.existing.objecto.my.namespace.MyClass
Objectifier.set('my.namespace.MyClass', {
	name: 'David'
}, some.existing.objecto); // Has to be an existing object

// Get an object
Objectifier.get('my.namespace.MyClassToo');

// Try to find an object, create it if it doesn't exist
Objectifier.get('my.namespace.MyClassThree', true);

// Check for existence
Objectifier.exists('my.namespace.MyClassToo'); // returns TRUE or FALSE


Notice I didn't extend the Object prototype; you could but we've moved on from that practice.

注意,我没有扩展Object原型。 您可以,但是我们已经从这种做法继续前进。

I use these functions on just about every project I work on.  I find them very useful when dealing with APIs, as you can never assume an object chain exists.  I wish I had included this code within my 7 Essential JavaScript Functions post!

我几乎在我从事的每个项目中都使用这些功能。 我发现它们在处理API时非常有用,因为您永远无法假设存在对象链。 我希望我的7个基本JavaScript函数帖子中包含了此代码!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值