
1、立即执行函数
(function() {// 代码})();
2、 闭包
function f1() {var N = 0; // N是f1函数的局部变量function f2() {// f2是f1函数的内部函数,是闭包N += 1; // 内部函数f2中使用了外部函数f1中的变量Nconsole.log(N);}return f2;}var result = f1();result(); // 输出1result(); // 输出2result(); // 输出3
3、使用闭包定义私有变量
function Product() {var name;this.setName = function(value) {name = value;};this.getName = function() {return name;};}var p = new Product();p.setName("Fundebug");console.log(p.name); // 输出undefinedconsole.log(p.getName()); // 输出Fundebug
4、prototype
function Rectangle(x, y) {this._length = x;this._breadth = y;}Rectangle.prototype.getDimensions = function() {return {length: this._length,breadth: this._breadth};};var x = new Rectangle(3, 4);var y = new Rectangle(4, 3);console.log(x.getDimensions()); // { length: 3, breadth: 4 }console.log(y.getDimensions()); // { length: 4, breadth: 3 }
5、模块化
var module = (function() {var N = 5;function print(x) {console.log("The result is: " + x);}function add(a) {var x = a + N;print(x);}return {description: "This is description",add: add};})();console.log(module.description); // 输出"this is description"module.add(5); // 输出“The result is: 10”
6、变量提升
console.log(y); // 输出undefinedy = 2; // 初始化y
var y; // 声明yconsole.log(y); // 输出undefinedy = 2; // 初始化y
7、柯里化
var add = function(x) {return function(y) {return x + y;};};console.log(add(1)(1)); // 输出2var add1 = add(1);console.log(add1(1)); // 输出2var add10 = add(10);console.log(add10(1)); // 输出11
8、apply, call与bind方法
var user = {name: "Rahul Mhatre",whatIsYourName: function() {console.log(this.name);}};user.whatIsYourName(); // 输出"Rahul Mhatre",var user2 = {name: "Neha Sampat"};user.whatIsYourName.call(user2); // 输出"Neha Sampat"
-
apply(thisArg, [argsArray])
-
call(thisArg, arg1, arg2, …)
var user = {greet: "Hello!",greetUser: function(userName) {console.log(this.greet + " " + userName);}};var greet1 = {greet: "Hola"};user.greetUser.call(greet1, "Rahul"); // 输出"Hola Rahul"user.greetUser.apply(greet1, ["Rahul"]); // 输出"Hola Rahul"
var user = {greet: "Hello!",greetUser: function(userName) {console.log(this.greet + " " + userName);}};var greetHola = user.greetUser.bind({greet: "Hola"});var greetBonjour = user.greetUser.bind({greet: "Bonjour"});greetHola("Rahul") // 输出"Hola Rahul"greetBonjour("Rahul") // 输出"Bonjour Rahul"
9、Memoization
function memoizeFunction(func) {var cache = {};return function() {var key = arguments[0];if (cache[key]) {return cache[key];} else {var val = func.apply(this, arguments);cache[key] = val;return val;}};}var fibonacci = memoizeFunction(function(n) {return n === 0 || n === 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);});console.log(fibonacci(100)); // 输出354224848179262000000console.log(fibonacci(100)); // 输出354224848179262000000
10、函数重载
function addMethod(object, name, f) {var old = object[name];object[name] = function() {// f.length为函数定义时的参数个数// arguments.length为函数调用时的参数个数if (f.length === arguments.length) {return f.apply(this, arguments);} else if (typeof old === "function") {return old.apply(this, arguments);}};}// 不传参数时,返回所有namefunction find0() {return this.names;}// 传一个参数时,返回firstName匹配的namefunction find1(firstName) {var result = [];for (var i = 0; i < this.names.length; i++) {if (this.names[i].indexOf(firstName) === 0) {result.push(this.names[i]);}}return result;}// 传两个参数时,返回firstName和lastName都匹配的namefunction find2(firstName, lastName) {var result = [];for (var i = 0; i < this.names.length; i++) {if (this.names[i] === firstName + " " + lastName) {result.push(this.names[i]);}}return result;}var people = {names: ["Dean Edwards", "Alex Russell", "Dean Tom"]};addMethod(people, "find", find0);addMethod(people, "find", find1);addMethod(people, "find", find2);console.log(people.find()); // 输出["Dean Edwards", "Alex Russell", "Dean Tom"]console.log(people.find("Dean")); // 输出["Dean Edwards", "Dean Tom"]console.log(people.find("Dean", "Edwards")); // 输出["Dean Edwards"]

荐
阅
读
如何优雅的导出 Excel
5. 团队开发中 Git 最佳实践

在看

本文分享自微信公众号 - Java后端(web_resource)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
相关文章
暂无评论...
