Returning function模式

本文深入探讨了JavaScript中闭包的概念,通过实例展示了如何在闭包中存储私有数据,以及如何确保这些数据的隔离性和安全性。通过分析setup函数和next变量的操作,读者将了解闭包在实际编程中的应用,特别是如何利用其特性来创建局部作用域和维护状态。
// setup 函数能在一个闭包里存储一些私有数据
// setup() 创建了一个技术函数, 返回下一个id. 并且count变量没有暴漏出来.
var setup = function () {
var count = 0;
return function () {
return ++count;
};
};
var next = setup(); //修改next无法改变count的值,count是受到保护的
//next(); // returns 1
//next(); // returns 2
//next(); // returns 3
..\Drive\Include\usart.h(15): error: #79: expected a type specifier void USART_SendString(USART_TypeDef* USARTx, char *str); ..\Drive\Include\usart.h(15): error: #90: function returning function is not allowed void USART_SendString(USART_TypeDef* USARTx, char *str); ..\Drive\Include\usart.h(17): warning: #1-D: last line of file ends without a newline #endif ..\MY\main.c(18): warning: #223-D: function "printf" declared implicitly printf(buffer, "Temperature: %dC Humidity: %d%%\r\n", temp, humi); ..\MY\main.c(19): error: #167: argument of type "USART_TypeDef *" is incompatible with parameter of type "<error-type> (*)(USART_TypeDef *)" USART_SendString(USART1, buffer); ..\MY\main.c(23): error: #167: argument of type "USART_TypeDef *" is incompatible with parameter of type "<error-type> (*)(USART_TypeDef *)" USART_SendString(USART1, "Read Error!\r\n"); ..\MY\main.c(25): warning: #223-D: function "Delay_ms" declared implicitly Delay_ms(2000); // 使用SysTick实现的延时 ..\MY\main.c(27): warning: #1-D: last line of file ends without a newline } ..\MY\main.c: 5 warnings, 4 errors compiling usart.c... ..\Drive\Include\usart.h(15): error: #79: expected a type specifier void USART_SendString(USART_TypeDef* USARTx, char *str); ..\Drive\Include\usart.h(15): error: #90: function returning function is not allowed void USART_SendString(USART_TypeDef* USARTx, char *str); ..\Drive\Include\usart.h(17): warning: #1-D: last line of file ends without a newline #endif ..\Drive\Source\usart.c(36): error: #79: expected a type specifier void USART_SendString(USART_TypeDef* USARTx, char *str) ..\Drive\Source\usart.c(36): error: #90: function returning function is not allowed void USART_SendString(USART_TypeDef* USARTx, char *str) ..\Drive\Source\usart.c(36): error: #141-D: unnamed prototyped parameters not allowed when body is present void USART_SendString(USART_TypeDef* USARTx, char *str) ..\Drive\Source\usart.c(42): warning: #1-D: last line of file ends without a newline } ..\Drive\Source\usart.c: 2 warnings, 5 errors
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值