!function(){}()

常见格式:(function() { /* code */ })();
解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号内为匿名函数的参数。
作用:可以用它创建命名空间,只要把自己所有的代码都写在这个特殊的函数包装内,那么外部就不能访问,除非你允许(变量前加上window,这样该函数或变量就成为全局)。各JavaScript库的代码也基本是这种组织形式。
// 如果你不在意返回值,或者不怕难以阅读
// 你甚至可以在function前面加一元操作符号

!function () { /* code */ } ();
~function () { /* code */ } ();
-function () { /* code */ } ();
+function () { /* code */ } ();

都是跟(function(){})();这个函数是一个意思,都是告诉浏览器自动运行这个匿名函数的,因为!+()这些符号的运算符是最高的,所以会先运行它们后面的函数

转载于:https://www.cnblogs.com/1032473245jing/p/8193607.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完成CTime时间类和CDate日期类的设计,使得主程序能够正确运行。 时间类CTime包含私有成员数据时(hour)分(minute)秒(second),均为int类型。 CDate日期类由CTime时间类公有派生而来,包含私有成员数据年(year)月(month)日(day),均为int类型。 main函数已给定,提交时只需要提交main函数外的代码部分。 #include<iostream> using namespace std; //你提交的代码在这里 int main() { int dy,dm,dd,th,tm,ts; cin>>dy>>dm>>dd>>th>>tm>>ts; CTime t1; cout<<"[T1]"; t1.Show(); CDate d1; cout<<"[D1]"; d1.Show(); CDate d2(dy,dm,dd); cout<<"[D2]"; d2.Show(); CDate d3(dy,dm,dd,th,tm,ts); cout<<"[D3]"; d3.Show(); CDate d4(dy,dm,dd,t1); cout<<"[D4]"; d4.Show(); CDate d5(t1); cout<<"[D5]"; d5.Show(); return 0; } Input 1行,包含6个整数,分别代表年,月,日,时,分和秒。 Output 输出包含4行,具体输出格式参照样例。 Sample Input 1 1949 10 1 16 28 37 Sample Output 1 Function #1 is called! [T1]9:10:11 Function #3 is called! Function #1 is called! Function #4 is called! [D1]2023-4-5 9:10:11 Function #3 is called! Function #9 is called! Function #1 is called! Function #5 is called! [D2]1949-10-1 9:10:11 Function #3 is called! Function #9 is called! Function #2 is called! Function #6 is called! [D3]1949-10-1 16:28:37 Function #3 is called! Function #9 is called! Function #0 is called! Function #7 is called! [D4]1949-10-1 9:10:11 Function #3 is called! Function #9 is called! Function #0 is called! Function #8 is called! [D5]2000-12-31 9:10:11 Function #3 is called! Function #9 is called!
06-10
``` #include<iostream> using namespace std; class CTime { private: int hour, minute, second; public: CTime() { hour = 9; minute = 10; second = 11; cout << "Function #1 is called! "; } CTime(int h, int m, int s) { hour = h; minute = m; second = s; cout << "Function #0 is called! "; } void Show() { if (hour < 10) cout << 0; cout << hour << ":"; if (minute < 10) cout << 0; cout << minute << ":"; if (second < 10) cout << 0; cout << second << endl; } }; class CDate : public CTime { private: int year, month, day; public: CDate() : CTime() { year = 2023; month = 4; day = 5; cout << "Function #2 is called! "; } CDate(int y, int m, int d) : CTime() { year = y; month = m; day = d; cout << "Function #6 is called! "; } CDate(int y, int m, int d, int h, int mi, int s) : CTime(h, mi, s) { year = y; month = m; day = d; cout << "Function #7 is called! "; } CDate(int y, int m, int d, CTime t) : CTime(t) { year = y; month = m; day = d; cout << "Function #8 is called! "; } CDate(CTime t) : CTime(t) { year = 2000; month = 12; day = 31; cout << "Function #5 is called! "; } void Show() { cout << year << "-"; if (month < 10) cout << 0; cout << month << "-"; if (day < 10) cout << 0; cout << day << " "; CTime::Show(); cout << "Function #9 is called! "; } }; int main() { int dy, dm, dd, th, tm, ts; cin >> dy >> dm >> dd >> th >> tm >> ts; CTime t1; cout << "[T1]"; t1.Show(); CDate d1; cout << "[D1]"; d1.Show(); CDate d2(dy, dm, dd); cout << "[D2]"; d2.Show(); CDate d3(dy, dm, dd, th, tm, ts); cout << "[D3]"; d3.Show(); CDate d4(dy, dm, dd, t1); cout << "[D4]"; d4.Show(); CDate d5(t1); cout << "[D5]"; d5.Show(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值