<?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/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>javascript-class &#8211; MaxVergelli.com</title>
	<atom:link href="https://www.maxvergelli.com/tag/javascript-class/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.maxvergelli.com</link>
	<description>sourcecode repository</description>
	<lastBuildDate>Tue, 14 Apr 2020 17:58:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i2.wp.com/www.maxvergelli.com/wp-content/uploads/2020/04/wp-1586823685141.png?fit=32%2C32&#038;ssl=1</url>
	<title>javascript-class &#8211; MaxVergelli.com</title>
	<link>https://www.maxvergelli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">34603511</site>	<item>
		<title>Javascript equivalent to PHP __construct</title>
		<link>https://www.maxvergelli.com/javascript-equivalent-to-php-__construct/</link>
					<comments>https://www.maxvergelli.com/javascript-equivalent-to-php-__construct/#respond</comments>
		
		<dc:creator><![CDATA[Max]]></dc:creator>
		<pubDate>Thu, 16 Apr 2020 17:36:00 +0000</pubDate>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[class-constructor]]></category>
		<category><![CDATA[construct]]></category>
		<category><![CDATA[constructor]]></category>
		<category><![CDATA[constructor-function]]></category>
		<category><![CDATA[constructor-method]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript-class]]></category>
		<category><![CDATA[javascript-construct]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[parameters]]></category>
		<category><![CDATA[php-like]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scripting]]></category>
		<guid isPermaLink="false">https://www.maxvergelli.com/?p=245</guid>

					<description><![CDATA[Constructor method in Javascript class equivalent to PHP, how to run a function in Javascript whenever the object is instantiated.]]></description>
										<content:encoded><![CDATA[
<p>The <code>constructor</code> method is a special method for creating and initializing an object created within a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class"><code>class</code></a>. In other words, it&#8217;s a function that runs automatically whenever the object is instantiated.</p>



<p><strong>Using the <code>constructor</code> method</strong></p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}">class Person {

  constructor(name) {
    this.name = name;
  }

  introduce() {
    console.log(`Hello, my name is ${this.name}`);
  }

}

const otto = new Person('Otto');

otto.introduce();</pre></div>



<p>If you don&#8217;t provide your own constructor, then a default constructor will be supplied for you. If your class is a base class, the default constructor is empty:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}">constructor() {}</pre></div>



<p>If your class is a derived class, the default constructor calls the parent constructor, passing along any arguments that were provided:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}">constructor(...args) {
  super(...args);
}</pre></div>



<p>Just as a reminder: the <strong>super</strong> keyword is used to access and call functions on an object&#8217;s parent (more information here <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super" class="aioseop-link">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super</a>)</p>



<p>Look at these other resources on <strong>Javascript constructor</strong>:</p>



<p><a href="https://stackoverflow.com/questions/61213185/javascript-equivalent-to-php-construct">https://stackoverflow.com/questions/61213185/javascript-equivalent-to-php-construct</a></p>



<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.maxvergelli.com/javascript-equivalent-to-php-__construct/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">245</post-id>	</item>
	</channel>
</rss>
