




Si vous avez souvent regarder les films de HBO , puis le titre de ce post JavaScript vous ?tre familier : Oui , c'est vrai , je veux emprunter ce slogan de HBO pour d?crire le contenu de ce post : il vous fournira les concepts de base JavaScript , et vous serez en mesure d' acc?der , de comprendre la programmation web langage JavaScript facilement et rapidement
Exemple, les codes JavaScript pour essayer ? l'heure actuelle , cet article JavaScript tutorial nous 5 pi?ces et je mettrai ? jour s'il ya nouveau chapitre , en attendant , vous pouvez passer par :
? D?clarations de fonctions JavaScript et des expressions de fonction JavaScript - Concepts de base
? JavaScript Prototype : Quelques concepts de base
? 5 Chef Concepts H?ritage JavaScript
? Concepts simples sur les types et les objets en JavaScript POO
? Top 10 Meilleurs livres JavaScript que les d?butants devraient apprendre
While the Javascript language offers many of the constructs required for object-oriented programming, they remain largely unused. Today we’ll take a look at how to start with object-oriented programing in Javascript. by defining a class in Javascript. We’ll use the simple HTML file to call our script file,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Hello Javascript!</title> <script type="text/javascript" src="MyScript.js"></script> </head> <body> Hello Javascript! </body> </html>
Now let’s create a class definition (in MyScript.js). Note that there is no ‘class’ keyword in Javascript, the class definition is just a function definition,
function MyClass() { // Public field this.aPublicField = "This is a public field of the type MyClass"; // Private field var aPrivateField = "This is a private field of the type MyClass"; // Public method this.aPublicMethod = function() { // Use the private method if (aPrivateMethod()) { return this.aPublicField; } else { return aPrivateField; } } // Private method function aPrivateMethod() { return true; } // Oops! We can expose the private field this.exposePrivateField = aPrivateField; // and the private method this.exposePrivateMethod = aPrivateMethod; } // Create an instance of MyClass using the ‘new’ keyword var myclass = new MyClass(); // Get the public field alert(myclass.aPublicField); // Call the public method alert(myclass.aPublicMethod()); // Call the private field – can’t get to them directly alert(myclass.exposePrivateField); alert(myclass.exposePrivateMethod());
So it’s pretty simple to create a class in Javascript. We can also create a runtime field for the class,
// Create an instance of MyClass var myclass = new MyClass(); // Create a field at runtime myclass.aRuntimeField = "This is a runtime field of the type MyClass"; // View the field value alert(myclass.aRuntimeField);
Now that we created a base Javascript class in the first post, let’s inherit from it to get a derived class.
// Define the derived function (remember there is no 'class' keyword in Javascript) function MyDerivedClass() { } // Derive from MyClass, equivalent to –> public class MyDerivedClass : MyClass MyDerivedClass.prototype = new MyClass(); // Create an instance of the derived class var myderivedClass = new MyDerivedClass(); // Get the public field alert(myderivedClass.aPublicField); // Call the public method alert(myderivedClass.aPublicMethod());
Woah! That was simple! Notice that MyClass’s private methods and the runtime field (aRuntimeField) we gave to MyClass in the last post is not available to MyDerivedClass.
Let’s override the base classe’s public fields and methods and see what happens,
// Define the derived function (remember there is no 'class' keyword in Javascript function MyDerivedClass() { // Override the base's public field this.aPublicField = "This is a public field of the type MyDerivedClass"; // Override the base's public method this.aPublicMethod = function() { return this.aPublicField; } } // Derive from MyClass, equivalent to –> public class MyDerivedClass : MyClass MyDerivedClass.prototype = new MyClass(); // Create an instance of the derived class var myderivedClass = new MyDerivedClass(); // Get the public field alert(myderivedClass.aPublicField); // Call the public method alert(myderivedClass.aPublicMethod());
You’ll see that the base classes public fields and methods have been overridden in the derived class.
We can obviously derive again,
// Derive from MyDerivedClass function MyDerivedDerivedClass() { // Override the base's public field this.aPublicField = "This is a public field of the type MyDerivedDerivedClass"; // Override the base's public method this.aPublicMethod = function() { return this.aPublicField; } } // Derive from MyDerivedClass, equivalent to –> public class MyDerivedDerivedClass : MyDerivedClass MyDerivedDerivedClass.prototype = new MyDerivedClass(); // Create an instance of the derived class var myderivedderivedClass = new MyDerivedDerivedClass(); // Call the public method alert(myderivedderivedClass.aPublicField);
Javascript is ... Réponse
Réponse
Jquery instead of javascript ... @ Cao Phong Réponse
For example : Javascript:
document.style.color = "red"; --> so complicated to remember
Jquery:
$(div).css ("color","red"); ->is that okey for coding ????