Js中prototype 、$.fn、“:”、jQuery.prototype.init 是什么,表达什么意思

一、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() 直接调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值