Constructeurs D'objets JavaScript
Constructeurs D'objets JavaScript
M A N U S C R I T A N G U L A I R E G I T P o s t g r e S Q L M O N G O D B A S P I C I A A L L E R K O T L I N T O U P E T V U E D S A G É N É R A T I O N I A S C I P Y A W S L A C Y B E R - S É C U R I T É S C I E N C E S D E S D O N N É E S
Exemple
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
Essayez-le vous-même »
Remarques
Il est considéré comme une bonne pratique de nommer les fonctions du
constructeur avec une première lettre majuscule.
À propos de ça
Dans un constructeur, la fonction thisn'a pas de valeur. C'est un substitut au
nouvel objet. La valeur de thisdeviendra le nouvel objet lorsqu'un nouvel objet
sera créé.
Voir également:
Le tutoriel JavaScript ce
Parfois, nous avons besoin d'un « plan » pour créer de nombreux objets du
même « type ».
La façon de créer un "type d'objet" consiste à utiliser une fonction
constructeur d'objet .
Essayez-le vous-même »
PUBLICITÉ
Quel objet dépend de la manière dont thisil est invoqué (utilisé ou appelé).
Note
thisn'est pas une variable. C'est un mot clé. Vous ne pouvez pas modifier la
valeur de this.
Voir également:
Le tutoriel JavaScript ce
Exemple
myFather.nationality = "English";
Essayez-le vous-même »
Essayez-le vous-même »
Exemple
Person.nationality = "English";
Essayez-le vous-même »
Exemple
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English";
}
Essayez-le vous-même »
De cette façon, les propriétés des objets peuvent avoir des valeurs par
défaut.
Ajout d'une méthode à un constructeur
Votre fonction constructeur peut également définir des méthodes :
Exemple
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.name = function() {
return this.firstName + " " + this.lastName;
};
}
Essayez-le vous-même »
You cannot add a new method to an object constructor the same way you
add a new method to an existing object.
Example
function Person(firstName, lastName, age, eyeColor) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.eyeColor = eyeColor;
this.changeName = function (name) {
this.lastName = name;
};
}
Try it Yourself »
Try it Yourself »
The Math() object is not in the list. Math is a global object. The new keyword
cannot be used on Math.
Example
let x1 = ""; // new primitive string
let x2 = 0; // new primitive number
let x3 = false; // new primitive boolean
const x4 = {}; // new Object object
const x5 = []; // new Array object
const x6 = /()/ // new RegExp object
const x7 = function(){}; // new function
Try it Yourself »
String Objects
Normally, strings are created as primitives: firstName = "John"
But strings can also be created as objects using the new keyword:
firstName = new String("John")
Learn why strings should not be created as object in the chapter JS Strings.
Number Objects
Normally, numbers are created as primitives: x = 30
But numbers can also be created as objects using the new keyword:
x = new Number(30)
Boolean Objects
Normally, booleans are created as primitives: x = false
But booleans can also be created as objects using the new keyword:
x = new Boolean(false)
ADVERTISEMENT
COLOR PICKER
ADVERTISEMENT
SPACES
UPGRADE
AD-FREE
NEWSLETTER
GET CERTIFIED
CONTACT US
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples
Exemples HTML Exemples
CSS Exemples
JavaScript Exemples
pratiques Exemples
SQL
Exemples Python Exemples
W3.CSS Exemples
d'amorçage Exemples
PHP Exemples
Java Exemples
XML Exemples
jQuery
Obtenir une certification
Certificat HTML Certificat
CSS Certificat
JavaScript Certificat
Front End Certificat
SQL Certificat
Python Certificat
PHP Certificat
jQuery Certificat
Java Certificat
C++ Certificat
C# Certificat
XML
FORUM SUR LA SALLE DE CLASSE
W3Schools est optimisé pour l'apprentissage et la formation. Les exemples pourraient être
simplifiés pour améliorer la lecture et l’apprentissage. Les didacticiels, références et exemples sont
constamment révisés pour éviter les erreurs, mais nous ne pouvons garantir l'exactitude totale de
tout le contenu. En utilisant W3Schools, vous acceptez d'avoir lu et accepté nos conditions
d'utilisation , nos cookies et notre politique de confidentialité .
Copyright 1999-2024 par Refsnes Data. Tous droits réservés. W3Schools est propulsé par
W3.CSS .