- Demo
- Agrandir
- Recharger
- New window
G�n�rez vos vid�os d'entreprise par l'IA avec la voix ou simplement du texte
Votre premi�re application vid�o AI GRATUITE ! Automatisez votre premi�re vid�o AI. Cr�ez votre vid�o professionnelle en 5 minutes gr�ce � l'IA. Aucun �quipement ni comp�tence en montage vid�o requis. Production vid�o sans effort pour les sp�cialistes du marketing de contenu.
Using a Constructor Function.
Let's use our previous constructor function and build upon it so it performs some basic (but dynamic) operations when we instantiate it.
function myObject(){ this.iAm = 'an object'; this.whatAmI = function(){ alert('I am ' + this.iAm); }; };
Just like any JavaScript function, we can use arguments with our constructor function;
function myObject(what){ this.iAm = what; this.whatAmI = function(language){ alert('I am ' + this.iAm + ' of the ' + language + ' language'); }; };
Now let's instantiate our object and call its whatAmI method, filling in the required fields as we do so.
var myNewObject = new myObject('an object'); myNewObject.whatAmI('JavaScript');
This will alert 'I am an object of the JavaScript language.'
To Instantiate or not to Instantiate
I mentioned earlier about the differences between Object Constructors and Object Literals and that when a change is made to an Object Literal it affects that object across the entire script, whereas when a Constructor function is instantiated and then a change is made to that instance, it won't affect any other instances of that object. Let's try an example;
First we will create an Object literal;
var myObjectLiteral = { myProperty : 'this is a property' } //alert current myProperty alert(myObjectLiteral.myProperty); //this will alert 'this is a property' //change myProperty myObjectLiteral.myProperty = 'this is a new property'; //alert current myProperty alert(myObjectLiteral.myProperty); //this will alert 'this is a new property', as expected
Even if you create a new variable and point it towards the object, it will have the same effect.
var myObjectLiteral = { myProperty : 'this is a property' } //alert current myProperty alert(myObjectLiteral.myProperty); //this will alert 'this is a property' //define new variable with object as value var sameObject = myObjectLiteral; //change myProperty myObjectLiteral.myProperty = 'this is a new property'; //alert current myProperty alert(sameObject.myProperty); //this will still alert 'this is a new property'
Now let's try a similar exercise with a Constructor function.
//this is one other way of creating a Constructor function var myObjectConstructor = function(){ this.myProperty = 'this is a property' } //instantiate our Constructor var constructorOne = new myObjectConstructor(); //instantiate a second instance of our Constructor var constructorTwo = new myObjectConstructor(); //alert current myProperty of constructorOne instance alert(constructorOne.myProperty); //this will alert 'this is a property' //alert current myProperty of constructorTwo instance alert(constructorTwo.myProperty); //this will alert 'this is a property'
So as expected, both return the correct value, but let's change the myProperty for one of the instances.
//this is one other way of creating a Constructor function var myObjectConstructor = function(){ this.myProperty = 'this is a property' } //instantiate our Constructor var constructorOne = new myObjectConstructor(); //change myProperty of the first instance constructorOne.myProperty = 'this is a new property'; //instantiate a second instance of our Constructor var constructorTwo = new myObjectConstructor(); //alert current myProperty of constructorOne instance alert(constructorOne.myProperty); //this will alert 'this is a new property' //alert current myProperty of constructorTwo instance alert(constructorTwo.myProperty); //this will still alert 'this is a property'
As you can see from this example, even though we changed the property of constructorOne it didn't affect myObjectConstructor and therefore didn't affect constructorTwo. Even if constructorTwo was instantiated before we changed the myProperty property of constructorOne, it would still not affect the myProperty property of constructorTwo as it is a completely different instance of the object within JavaScript's memory.
So which one should you use? Well it depends on the situation, if you only need one object of its kind for your script (as you will see in our example at the end of this article), then use an object literal, but if you need several instances of an object, where each instance is independent of the other and can have different properties or methods depending on the way it's constructed, then use a constructor function.
- Sent (0)
- Nouveau
Générez vos vidéos d'entreprise par l'IA avec la voix ou simplement du texte
chatGPTaz.com
Parlez à ChatGPT dans votre langue maternelle
AppAIVidéo
Votre première application vidéo AI GRATUITE
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app
Your next top AI Assistant
Claude AI, developed by Anthropic
Your next top AI Assistant
Claude AI, developed by Anthropic
Temu gratuit 500 $ pour les nouveaux utilisateurs
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Crédits publicitaires TikTok gratuits
Maîtrisez les publicités TikTok pour le marketing de votre entreprise
Dall-E-OpenAI.com
Générez automatiquement des images créatives avec l'IA
chatGPT4.win
Parlez à ChatGPT dans votre langue maternelle
Premier produit d'intelligence artificielle d'Elon Musk - Grok/UN.com
Parlez au chatbot Grok AI dans votre langue
Outily.win
Centre d'outils ouvert et gratuit, utilisable par tous et pour tous, avec des centaines d'outils
GateIO.gomymobi.com
Airdrops gratuits à réclamer et à partager jusqu'à 150 000 $ par projet
iPhoneKer.com
Économisez jusqu'à 630 $ à l'achat d'un nouvel iPhone 16
Acheter le robot Tesla Optimus
Commandez votre robot Tesla Bot : Optimus Gen 2 dès aujourd'hui pour moins de 20 000 $