jQuey 插件开发 中篇

直接一份简单的代码

;(function($, window, document, undefined) {
	var YourName = function(ele, opt) {
		this.$element = ele;
		this.defaults = {
			'color': 'blue',
			'fontSize': '14px',
		}
		this.options = $.extend({}, this.defaults, opt);
	}

	YourName.prototype = {
		first: function() {
			return this.$element.css({
				'color': this.options.color,
				'fontSize': this.options.fontSize,
			});
		}
	}

	$.fn.TestName = function(options) {
		var Y = new YourName(this, options);
		return Y.first();
	}
})(jQuery, window, document)

匿名函数的一个封装, 你肯定有很多的疑问, 问什么最前面要有";", 为什么匿名函数里面要还有几个参数……, 下面为会一一讲解

首先 ";": 

1、防止多文件集成成一个文件后,高压缩出现语法错误。

2、匿名函数保护内部变量

 

posted @ 2016-03-28 16:43  方达达  阅读(100)  评论(0编辑  收藏  举报