惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
V2EX - 技术
V2EX - 技术
The Last Watchdog
The Last Watchdog
宝玉的分享
宝玉的分享
T
Tenable Blog
WordPress大学
WordPress大学
K
Kaspersky official blog
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hacker News - Newest:
Hacker News - Newest: "LLM"
P
Palo Alto Networks Blog
Help Net Security
Help Net Security
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
Forbes - Security
Forbes - Security
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
H
Hacker News: Front Page
W
WeLiveSecurity
博客园 - 【当耐特】
G
Google Developers Blog
Martin Fowler
Martin Fowler
TaoSecurity Blog
TaoSecurity Blog
Hacker News: Ask HN
Hacker News: Ask HN
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
AI
AI
N
Netflix TechBlog - Medium
C
Cisco Blogs
I
Intezer
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
L
Lohrmann on Cybersecurity
Engineering at Meta
Engineering at Meta

博客园 - shitou

java解压缩.Z文件 Uncompress a unix compressed file--.Z poi jxl 生成EXCEL 正则表达式删除空行 中文乱码在java中URLEncoder.encode方法要调用两次解决 数据层框架guzz Maven使用手册 Apache+Jetty整合 Jetty 的配置 jetty 网址分享 windows安装MongoDB Spring中使用Quartz Quartz 在Spring中动态设置cronExpression spring任务调度-Quartz Quartz的cron表达式 spring3.0核心包说明 Struts2 异常错误总结 ExtJS的Desktop应用 java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.ValueStack.findValue(Ljava/lang/String;Ljava/lang/Class;Z)
JS的构造函数
shitou · 2011-07-07 · via 博客园 - shitou

JS的构造函数

 1 //构造函数
 2 //使自己的对象多次复制,同时实例根据设置的访问等级可以访问其内部的属性和方法
 3 //当对象被实例化后,构造函数会立即执行它所包含的任何代码
 4  function myObject(msg){
 5     //特权属性(公有属性)
 6      this.myMsg = msg; //只在被实例化后的实例中可调用
 7     this.address = '上海';
 8     
 9     //私有属性
10     var name = '豪情';
11     var age = 29;
12     var that = this;
13     
14     //私有方法
15     function sayName(){
16         alert(that.name);
17     }
18     //特权方法(公有方法)
19     //能被外部公开访问
20     //这个方法每次实例化都要重新构造而prototype是原型共享,所有实例化后,都共同引用同一个
21     this.sayAge = function(){
22         alert(name); //在公有方法中可以访问私有成员
23     }
24     //私有和特权成员在函数的内部,在构造函数创建的每个实例中都会包含同样的私有和特权成员的副本,
25     //因而实例越多占用的内存越多
26 }
27 //公有方法
28 //适用于通过new关键字实例化的该对象的每个实例
29 //向prototype中添加成员将会把新方法添加到构造函数的底层中去
30 myObject.prototype.sayHello = function(){
31     alert('hello everyone!');
32 }
33 //静态属性
34 //适用于对象的特殊实例,就是作为Function对象实例的构造函数本身
35 myObject.name = 'china';
36 //静态方法
37 myObject.alertname = function(){
38     alert(this.name);
39 }
40 //实例化
41 var m1 = new myObject('111');
42 //---- 测试属性 ----//
43 //console.log(myObject.name); //china
44 //console.log(m1.name); //undefined, 静态属性不适用于一般实例
45 //console.log(m1.constructor.name); //china, 想访问类的静态属性,先访问该实例的构造函数,然后在访问该类静态属性
46 //console.log(myObject.address); //undefined, myObject中的this指的不是函数本身,而是调用address的对象,而且只能是对象
47 //console.log(m1.address); //上海 此时this指的是实例化后的m1
48 
49 //---- 测试方法 ----//
50 //myObject.alertname(); //china,直接调用函数的类方法
51 //m1.alertname(); //FF: m1.alertname is not a function, alertname 是myObject类的方法,和实例对象没有直接关系
52 //m1.constructor.alertname(); //china, 调用该对象构造函数(类函数)的方法(函数)
53 //m1.sayHello(); //hello everyone, myObject类的prototype原型下的方法将会被实例继承
54 //myObject.sayHello(); //myObject.sayHello is not a function,sayHello是原型方法,不是类的方法
55 
56 //---- 测试prototype ----//
57 //console.log(m1.prototype); //undefined, 实例对象没有prototype
58 //console.log(myObject.prototype); //Object 
59 //alert(myObject.prototype.constructor); //console.log返回myObject(msg),此时alert()更清楚,相当于myObject
60 //console.log(myObject.prototype.constructor.name); //china, 相当于myObject.name;

参考: