issue97:javascript
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue97:javascript [2015/06/04 12:17] – créée d52fr | issue97:javascript [2015/06/20 21:22] (Version actuelle) – [encadré page 21] fcm_-_ekel | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | In the first part, I talked about how JavaScript has evolved over the years, what is the current status of the language, and why it is so widely adopted by developers. In this part, I will focus on JavaScript Objects and functions. Since I received an email from Ray (one of our readers, thank you for contacting me) with some questions, I will address these at the end of the article in the Questions and Solutions section. | + | ====== 1 ====== |
+ | |||
+ | **In the first part, I talked about how JavaScript has evolved over the years, what is the current status of the language, and why it is so widely adopted by developers. In this part, I will focus on JavaScript Objects and functions. Since I received an email from Ray (one of our readers, thank you for contacting me) with some questions, I will address these at the end of the article in the Questions and Solutions section. | ||
JavaScript Objects | JavaScript Objects | ||
Ligne 10: | Ligne 12: | ||
myName = 123; | myName = 123; | ||
- | In the first case, the myName stores a string value; in the second case it should store an integer, 123, but JavaScript has a special type, Number (https:// | + | In the first case, the myName stores a string value; in the second case it should store an integer, 123, but JavaScript has a special type, Number (https:// |
- | Since JavaScript is a dynamic language, I can extend objects in any way I want. For example, if I create a new object: | + | Dans la première partie, je vous ai montré comment JavaScript a évolué au fil des ans, l' |
+ | |||
+ | Les Objets JavaScript | ||
+ | |||
+ | En JavaScript, tout est un objet, même les fonctions sont des objets, ce qui, si vous avez l' | ||
+ | |||
+ | JavaScript est un langage de programmation dynamique, ce qui signifie que, sans aucun problème, les objets peuvent avoir différentes valeurs attribuées lors de l' | ||
+ | |||
+ | var monNom = " | ||
+ | monNom = 123; | ||
+ | |||
+ | Dans le premier cas, la variable monNom stocke une chaîne de caractères ; dans le second cas, elle stocke un nombre entier, 123, mais JavaScript a un type spécial, Number (https:// | ||
+ | |||
+ | ====== 2 ====== | ||
+ | |||
+ | **Since JavaScript is a dynamic language, I can extend objects in any way I want. For example, if I create a new object: | ||
var myHouse = { nrOfRooms: | var myHouse = { nrOfRooms: | ||
Ligne 27: | Ligne 44: | ||
These result is the following object: | These result is the following object: | ||
- | Object {nrOfRooms: 3, size: 100, price: 1500, currency: " | + | Object {nrOfRooms: 3, size: 100, price: 1500, currency: " |
- | As you can see, there are three ways to create custom objects: | + | Puisque JavaScript est un langage dynamique, je peux étendre des objets comme je le souhaite. Par exemple, si je crée un nouvel objet : |
+ | |||
+ | var maMaison = { nbDePieces: | ||
+ | |||
+ | Il crée un objet : | ||
+ | |||
+ | Object {nbDePieces: | ||
+ | |||
+ | Étendons-le avec quelques propriétés : | ||
+ | |||
+ | maMaison.prix = 1500; | ||
+ | maMaison[" | ||
+ | |||
+ | Cela nous donne pour résultat l' | ||
+ | |||
+ | Object {nbDePieces: | ||
+ | |||
+ | ====== 3 ====== | ||
+ | |||
+ | **As you can see, there are three ways to create custom objects: | ||
• The first, also called JSON (JavaScript Object Notation), defines objects using the curly braces and specifies the properties and their values separated by a colon. | • The first, also called JSON (JavaScript Object Notation), defines objects using the curly braces and specifies the properties and their values separated by a colon. | ||
• The second option is to use the . (dot) operator, and write the name of the new property and assign a value to it. | • The second option is to use the . (dot) operator, and write the name of the new property and assign a value to it. | ||
Ligne 36: | Ligne 72: | ||
Functions may or may not have return values. In the case of the isBiggerThan() function, I have not specified a return type, nor that it will have a return value, but I could easily return a boolean value (true or false), JavaScript permits this. | Functions may or may not have return values. In the case of the isBiggerThan() function, I have not specified a return type, nor that it will have a return value, but I could easily return a boolean value (true or false), JavaScript permits this. | ||
- | As an exercise, you can create other objects which simulate real life objects, like a forest which has a function called plantTrees, and receives a parameter nrOfTrees, and it sums up the number of trees in the forest. Or it may store the different types of animals which live in the forest; the topic does not really matter, the idea is to get you familiarized with object notation and function creation, we will use this a lot. | + | As an exercise, you can create other objects which simulate real life objects, like a forest which has a function called plantTrees, and receives a parameter nrOfTrees, and it sums up the number of trees in the forest. Or it may store the different types of animals which live in the forest; the topic does not really matter, the idea is to get you familiarized with object notation and function creation, we will use this a lot.** |
- | Questions and Solutions | + | Comme vous pouvez le voir, il y a trois façons de créer des objets personnalisés : |
+ | • La première, appelée aussi JSON (JavaScript Object Notation), définit des objets en utilisant les accolades et spécifie les propriétés et leurs valeurs séparées par deux points. | ||
+ | • La deuxième option est d' | ||
+ | • La troisième option consiste à utiliser l' | ||
+ | |||
+ | Les fonctions peuvent ou avoir des valeurs de retour ou pas. Dans le cas de la fonction isBiggerThan(), | ||
+ | |||
+ | Comme exercice, vous pouvez créer d' | ||
+ | |||
+ | ====== 4 ====== | ||
+ | |||
+ | **Questions and Solutions | ||
Question: Ray asked how we can select some information from a file, especially a SQLite database using JavaScript. | Question: Ray asked how we can select some information from a file, especially a SQLite database using JavaScript. | ||
Ligne 45: | Ligne 92: | ||
There are two approaches, two scenarios. The first one is when you have the SQLite database available on the client machine, where the browser is running. In this case you can use the SQL.js (https:// | There are two approaches, two scenarios. The first one is when you have the SQLite database available on the client machine, where the browser is running. In this case you can use the SQL.js (https:// | ||
- | The second scenario is when the SQLite database is available only on the web server and the client is accessing the web server through the Internet. In this case, you need to have a server-side component (this can be node.js based) which reads the SQLite database, and, using HTTP requests, sends the data back to the client’s browser. In this case, the client side JavaScript is more complex, because it needs to use AJAX calls to load the data. Here is a sample node.js code which connects to a SQLite database, creates a new table if it does not exist, and inserts two new entries in the table. After the insert it queries the table and writes the data to the console. | + | The second scenario is when the SQLite database is available only on the web server and the client is accessing the web server through the Internet. In this case, you need to have a server-side component (this can be node.js based) which reads the SQLite database, and, using HTTP requests, sends the data back to the client’s browser. In this case, the client side JavaScript is more complex, because it needs to use AJAX calls to load the data. Here is a sample node.js code which connects to a SQLite database, creates a new table if it does not exist, and inserts two new entries in the table. After the insert it queries the table and writes the data to the console.** |
- | To execute the code on the previous page, you will need to have node.js (https:// | + | Questions et solutions |
+ | |||
+ | Question : Ray a demandé comment on peut sélectionner certaines informations à partir d'un fichier, en particulier une base de données SQLite, en utilisant JavaScript. | ||
+ | |||
+ | Solution : Il y a deux approches, deux scénarios. Le premier est lorsque la base de données SQLite est disponible sur la machine cliente, là où le navigateur s' | ||
+ | |||
+ | Le deuxième scénario est lorsque la base de données SQLite est uniquement disponible sur le serveur Web et que le client y accède par Internet. Dans ce cas, vous devez avoir un composant côté serveur (qui peut être basé sur node.js) qui lit la base de données SQLite, et, en utilisant des requêtes HTTP, envoie les données au navigateur du client. Dans ce cas, le Javascript côté client est plus complexe, car il a besoin d' | ||
+ | |||
+ | ====== 5 ====== | ||
+ | |||
+ | **To execute the code on the previous page, you will need to have node.js (https:// | ||
npm install sqlite3 –save | npm install sqlite3 –save | ||
Ligne 61: | Ligne 118: | ||
In future articles, I will present how to create a new HTTP server using node, and how to transfer data from the server side to the client side – everything using JavaScript. | In future articles, I will present how to create a new HTTP server using node, and how to transfer data from the server side to the client side – everything using JavaScript. | ||
- | I would be happy to hear from you; what are the topics in JavaScript which you would be interested in. Please feel free and email me your topic ideas, the same way as Ray did. Thanks again, Ray! | + | I would be happy to hear from you; what are the topics in JavaScript which you would be interested in. Please feel free and email me your topic ideas, the same way as Ray did. Thanks again, Ray!** |
+ | |||
+ | Pour exécuter le code de la page précédente, | ||
+ | |||
+ | npm install sqlite3 -save | ||
+ | |||
+ | Quand tout est en place, vous pouvez exécuter le programme en utilisant : | ||
+ | |||
+ | node sqlite_reader.js | ||
+ | |||
+ | En supposant que vous avez nommé votre fichier sqlite_reader.js. | ||
+ | |||
+ | La sortie devrait ressembler à celle représentée ci-dessus dans la ligne de commande (je l' | ||
+ | |||
+ | Dans les prochains articles, je vous dirai comment créer un nouveau serveur HTTP à l'aide de node et comment transférer des données depuis le côté serveur vers le côté client - le tout en utilisant JavaScript. | ||
+ | |||
+ | Je serais heureux de vous entendre ; dites-moi quels sont les sujets qui vous intéresseraient autour de JavaScript. Sentez-vous libres de m' | ||
+ | |||
+ | ====== encadré page 21 ====== | ||
+ | |||
+ | Regardons le code ci-dessous : | ||
+ | maMaison.getInformation = function() { | ||
+ | | ||
+ | et a " + this.nbDePieces + " pieces." | ||
+ | } | ||
+ | |||
+ | Si vous appelez cette fonction ainsi : | ||
+ | maMaison.getInformation(); | ||
+ | cela affichera : La maison fait 100 m2 et a 3 pieces. | ||
+ | |||
+ | Les fonctions peuvent avoir des paramètres: | ||
+ | maMaison.estPlusGrandeQue = function(autreMaison) { | ||
+ | | ||
+ | } | ||
+ | |||
+ | Nous pouvons appeler la fonction : | ||
+ | maMaison.estPlusGrandeQue({taille: | ||
+ | ou | ||
+ | maMaison.estPlusGrandeQue({taille: | ||
+ | |||
+ | Dans le premier cas, cela retourne vrai et dans le second faux. | ||
+ | Remarquez que j'ai passé un simple objet avec une propriété, | ||
+ | et le code a pu s' | ||
+ | maMaison.estPlusGrandeQue({taille: | ||
+ | devise:" | ||
+ | ou | ||
+ | var maisonDeMonAmi = {taille: | ||
+ | devise:" | ||
+ | maMaison.estPlusGrandeQue(maisonDeMonAmi); | ||
+ | Le résultat sera le même. | ||
+ | |||
+ | ====== commentaires de l' | ||
+ | |||
+ | create the db instance : ouvrir la base de donnees | ||
+ | |||
+ | create the table if the database file was missing : creer la table si le fichier est absent | ||
+ | |||
+ | create a parameterized, | ||
+ | |||
+ | run the statement twice : executer deux fois la requete | ||
+ | each ? in the statement is substituted with the parameter : chaque ? dans la requete est remplace par un parametre | ||
+ | |||
+ | close the statement : fermer la requete | ||
+ | |||
+ | query the database and log the result to the console : requeter la base et afficher le resultat sur la console |
issue97/javascript.1433413072.txt.gz · Dernière modification : 2015/06/04 12:17 de d52fr