一、prototype 属性
例:
<script type="text/javascript">
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
employee.prototype.salary=null;
bill.salary=20000;
document.write(bill.salary);
</script>
输出:20000
定义用法:prototype 属性使您有能力向对象添加属性和方法。
二、$.fn或jquery.fn
例:
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
},
jQuery.fn = jQuery.prototype ={
//....
//......
};
定义用法:$.fn是指jquery的命名空间,文中让其等于jquery.prototype,可以向jquery对象添加属性,和方法。
如扩展$.fn.abc(),即$.fn.abc()是对jquery扩展了一个abc方法,那么后面你的每一个jquery实例都可以引用这个方法了. 那么你可以这样子:$("#div").abc();
三、“:”
例
jQuery.fn = jQuery.prototype = {
init: function( selector, context, rootjQuery ) {
\\\\
\\\\
};
定义用法:将function,变量等用json的写法。上文init:function(){}可以直接调用:jquery.fn.init()。
例:
var o = {
x: function(){},
y: "aa",
z: obj
}
调用直接用o.x() ;o.aa即可;可以理解为class的调用。
总结:prototype 向对象添加属性和方法。$.fn是指jquery的命名空间,加上fn上的方法及属性会对每个jquery实例有效。
“:”冒号json的写法,类似于类的调用。
例如 js中html()方法:
jQuery = function( selector, context ) {
//....
//....
};
jQuery.fn = jQuery.prototype ={
//....
//....
};
jQuery.extend = jQuery.fn.extend = function() {
//...
//...
};
jQuery.fn.extend({
html: function( value ) {
//....
//...
}
});
由上可知:html()方法为jQuery利用prototype 属性添加的一个方法,所以可以像这样$("#id").html() 直接调用。