jQuery.extend使用的几种方式
1.jQuery.extend(源对象)
1 jQuery源代码:
2
3 if(length == i){
4 target = this;
5 --i;
6 }
7
8 示例1:
9
10 var person = {
11 sex : 'male',
12 showName : function(name){
13 alert("Name: " + name);
14 }
15 };
16 jQuery.extend(person); // 将person对象扩展到jQuery($)对象上
17 jQuery.showName("admin"); // Name: admin
18 $.showName("admin"); // Name: amdin
19 alert("Sex: " + $.sex); // Sex: male
2.jQuery.extend(目标对象,源对象)
1 var person = {
2 language : ['java', 'c++', 'sql'],
3 showName : function(name){
4 alert("Name: " + name);
5 }
6 };
7 var student = {
8 showNum : function(num){
9 alert("Num: " + num);
10 }
11 };
12 jQuery.extend(student, person); // 将person对象扩展到指定的student对象上
13 student.showName("admin");
14 alert(student.language);
3.jQuery.extend(boolean,源对象)
1 var person = {
2 language : ['java', 'c++', 'sql'],
3 showName : function(name){
4 alert("Name: " + name);
5 }
6 };
7 jQuery.extend(true, person); // 将person对象扩展jQuery对象上
8 alert($.language); // java, c++, sql
9 $.language.push('PL/SQL'); // 修改扩展后的对象
10 alert(person.language); // java, c++, sql
11 person.language.pop();
从上面例子中可以看出对$.language的修改不会影响到person中的language属性。这就是深度复制
4.jQuery.extend(boolean,目标对象,源对象)
1 var person = {
2 showName : function(name){
3 alert("Name: " + name);
4 }
5 };
6 var student = {
7 language : ["java", "c++", "javascript"],
8 showNum : function(num){
9 alert("Num: " + num);
10 }
11 };
12 var target = jQuery.extend(person, student);
13 alert(target.language); // java, c++, javascript
14 target.language.push("PL/SQL");
15 alert(student.language); // java, c++, javascript, PL/SQL
16 student.language.pop();
17 alert(target.language); // java, c++, javascript
18 var target2 = jQuery.extend(true, person, student);
19 alert(target2.language); // java, c++, javascript
20 target2.language.push("PL/SQL");
21 alert(student.language); // java, c++, javascript
22 student.language.pop();
23 alert(target2.language); // java, c++, javascript, PL/SQL
转载自http://www.jb51.net/article/61478.htm