<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>檬檬前端行 &#187; 私有属性</title>
	<atom:link href="http://www.frontendcodes.com/?feed=rss2&#038;tag=%E7%A7%81%E6%9C%89%E5%B1%9E%E6%80%A7" rel="self" type="application/rss+xml" />
	<link>http://www.frontendcodes.com</link>
	<description>路漫漫其修远兮</description>
	<lastBuildDate>Wed, 18 Mar 2015 10:05:27 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>模拟私有属性的一个不成熟的办法</title>
		<link>http://www.frontendcodes.com/?p=38</link>
		<comments>http://www.frontendcodes.com/?p=38#comments</comments>
		<pubDate>Sun, 23 Jan 2011 14:50:37 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[前端]]></category>
		<category><![CDATA[私有属性]]></category>

		<guid isPermaLink="false">http://www.frontendcodes.com/?p=38</guid>
		<description><![CDATA[抛砖引玉，希望能得到更好的办法来模拟私有属性。]]></description>
			<content:encoded><![CDATA[<p>在js的面向对象写法中，并没有特定的静态属性，私有属性和公有属性。</p>
<p>一般来说，静态属性是直接给构造函数增加一个属性。</p>
<p>function con(){}</p>
<p>con.staticProperty=1;</p>
<p>由此实现静态方法。</p>
<p>而实例中的属性，一般都是公有的。</p>
<p>function con(){}</p>
<p>con.prototype.firstProperty=1;</p>
<p>con.prototype.secondProperty=2;</p>
<p>var obj=new con();</p>
<p>console.log(obj.firstProperty);</p>
<p>console.log(obj.secondProperty);</p>
<p>如上例，两个属性，都可以被实例obj调用，因此都是公有属性。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>对于私有属性的实现，JS中并没有给出方法，事实上，JS本身并没有私有，公有之说。</p>
<p>实现JS的私有属性，只能通过一些“取巧的办法”.</p>
<p>也看过蛮多的“取巧”，比较流行的办法是通过闭包。</p>
<p>function con(){</p>
<p>var privateObj={};</p>
<p>this.publicMethod=function(){ alert(privateObj);}</p>
<p>}</p>
<p>var obj=new con();</p>
<p>console.log(obj.publicMethod);</p>
<p>console.log(obj.privateObj);</p>
<p>new的过程中，由于方法pubicMethod引用了con的内部对象privateObj，形成了闭包，使得privateObj成为了“私有”。</p>
<p>这个办法有两个缺点：1，使用了闭包，过多的使用闭包肯定是不好的。 2，方法必须写在con函数的内部，如果用prototype，则无法引用privateObj。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>前几天在写JS，突然想到一个办法，来分享讨论一下。。。</p>
<div id="_mcePaste">function con(){</div>
<div id="_mcePaste">return this.makePublic();</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">con.prototype.privateFunc=function(o){alert(o)};</div>
<div id="_mcePaste">con.prototype.publicFunc=function(o){alert(o+1)};</div>
<div id="_mcePaste">con.prototype.testFunc=function(o){this.privateFunc(o);}</div>
<div id="_mcePaste">con.prototype.makePublic=function(){</div>
<div id="_mcePaste">var self=this;</div>
<div id="_mcePaste">return {</div>
<div id="_mcePaste">publicFunc : function(){self.publicFunc.apply(self,[].slice.call(arguments))},</div>
<div id="_mcePaste">testFunc : function(){self.testFunc.apply(self,[].slice.call(arguments))},</div>
<div>constructor:con,</div>
<div>proto:self</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">var obj=new con();</div>
<div id="_mcePaste">obj.publicFunc(1);</div>
<div id="_mcePaste">obj.testFunc(1);</div>
<div id="_mcePaste">console.log(obj.privateFunc);</div>
<div>这段代码的本质，就是用new操作符时，如果构造函数返回了一个对象，则实例会被赋值为返回的对象 这个办法，来从所有的方法中，抓出几个来return作为公有的，其他的无法被访问，就变成了私有的方法。</div>
<div>我的这个办法也有两个缺点： 1，构造函数中，也必须写上return this.makePublic     2,原型链被打破了。</div>
<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</div>
<div>抛砖引玉，希望能得到更好的办法来模拟私有属性。</div>
]]></content:encoded>
			<wfw:commentRss>http://www.frontendcodes.com/?feed=rss2&#038;p=38</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
