js 观察者模式

<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
	var toString = {}.toString,
		isFunction = function( it ) {
			return toString.call( it ) == "[object Function]";
		},

		isString = function( it ) {
			return toString.call( it ) == "[object String]";
		};

	var hub = (function() {
		var identifier = 1,
			channels = {},
			counter = {};

		return {
			subscribe: function( name, listener ) {
				if ( !isString(name) || !isFunction(listener) ) {
					return;
				}

				var done = channels[name];

				if ( !done ) {
					done = channels[name] = {};
					counter[name] = 0;
				}

				done[identifier] = listener;
				counter[name] += 1;

				return [ name, identifier++ ];
			},

			publish: function( name ) {
				var done, args, index;

				if ( !name || !isString(name) ) {
					return;
				}

				done = channels[name];
				args = [].slice.call( arguments, 1 );

				for ( index in done ) {
					done[index].apply( this, args );
				}
			},

			unsubscribe: function unsubscribe( handle ) {
				var isa = isArray( handle ),
					name = isa ? handle[0] : handle,
					done;

				if ( !name || !isString(name) ) {
					return;
				}

				done = channels[name];

				if ( isa ) {
					counter[name] -= 1;
					delete done[handle[1]];
				}

				if ( !isa || counter[name] === 0 ) {
					delete channels[name];
					delete counter[name];
				}
			}
		};
	})();

	hub.subscribe('a', function(){
		console.log(123);
	});
	hub.subscribe('a', function(){
		console.log(456);
	});

	console.log(hub)

	hub.publish('a')

</script>
</body>
</html>

 https://github.com/shichuan/javascript-patterns/blob/master/design-patterns/observer.html

 

http://icodon.com/observer-pattern-of-js.html

 

http://amplifyjs.com/api/pubsub/

 

http://rolfzhang.com/articles/927.html

 

<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
(function( global, undefined ) {
var slice = [].slice,
	subscriptions = {};

var amplify = global.amplify = {
	publish: function( topic ) {
		var args = slice.call( arguments, 1 ),
			topicSubscriptions,
			subscription,
			length,
			i = 0,
			ret;

		if ( !subscriptions[ topic ] ) {
			return true;
		}

		topicSubscriptions = subscriptions[ topic ].slice();
		for ( length = topicSubscriptions.length; i < length; i++ ) {
			subscription = topicSubscriptions[ i ];
			ret = subscription.callback.apply( subscription.context, args );
			if ( ret === false ) {
				break;
			}
		}
		return ret !== false;
	},

	subscribe: function( topic, context, callback, priority ) {
		if ( arguments.length === 3 && typeof callback === "number" ) {
			priority = callback;
			callback = context;
			context = null;
		}
		if ( arguments.length === 2 ) {
			callback = context;
			context = null;
		}
		priority = priority || 10;

		var topicIndex = 0,
			topics = topic.split( /\s/ ),
			topicLength = topics.length,
			added;
		for ( ; topicIndex < topicLength; topicIndex++ ) {
			topic = topics[ topicIndex ];
			added = false;
			if ( !subscriptions[ topic ] ) {
				subscriptions[ topic ] = [];
			}
	
			var i = subscriptions[ topic ].length - 1,
				subscriptionInfo = {
					callback: callback,
					context: context,
					priority: priority
				};
	
			for ( ; i >= 0; i-- ) {
				if ( subscriptions[ topic ][ i ].priority <= priority ) {
					subscriptions[ topic ].splice( i + 1, 0, subscriptionInfo );
					added = true;
					break;
				}
			}

			if ( !added ) {
				subscriptions[ topic ].unshift( subscriptionInfo );
			}
		}

		return callback;
	},

	unsubscribe: function( topic, callback ) {
		if ( !subscriptions[ topic ] ) {
			return;
		}

		var length = subscriptions[ topic ].length,
			i = 0;

		for ( ; i < length; i++ ) {
			if ( subscriptions[ topic ][ i ].callback === callback ) {
				subscriptions[ topic ].splice( i, 1 );
				break;
			}
		}
	}
};

}(this));


	amplify.subscribe('a', function(){
		console.log(123);
	});
	amplify.subscribe('a', function(){
		console.log(456);
	});

	console.log(amplify)

	amplify.publish('a')

</script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值