函数封装进阶

函数封装进阶

函数封装进阶

详情见  webstorm test1-四阶段-21.1.15-封装进阶

  • 1 构造函数+原型+自执函数

  • 2 传对象出去,1 中的构造函数 new 对象出来,放在函数内传递出来

  • 3 2 创建的函数放在自执行函数里面隐藏,通过 window 全局对象暴露出去

*   (暴露的是方法,可以传参数,把外部写的数据,传递到 new 的里面)

  • *js 进阶:封装   –构造函数+原型+包裹函数+自执行函数+暴露包裹函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(function () {
//自执行函数
function outer(name, age) {
//包裹函数用于返回 内部真正函数的实例对象
function Person(name, age) {
//构造函数
this.name = name;
this.age = age;
}
Person.prototype.showName = function () {
//内部函数原型方法
alert(this.name);
};
return new Person(name, age); //返回实例对象
}
window.callOuter = outer; //通过window暴露 callOuter可获得Person的实例对象
})();

callOuter("张三", 10); //调用使用,结果是一个 Person对象

1.jQuery.extend(object);为扩展 jQuery 类本身.为类添加新的方法。 

2.jQuery.fn.extend(object);给 jQuery 对象添加方法。

jQuery.fn.extend = jQuery.prototype.extend

**>>给函数设置参数的默认值****。**

**   – 调用时传的参会覆盖这的值。**


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!