JavaScript is a dynamic language based on objects and is event-driven. Using object-oriented thinking
JavaScript is a dynamic language based on objects and is event-driven. From the perspective of Object-oriented thinking, it also has the concept of class. JavaScript does not have the class keyword, but is implemented using function.
1. Implementation Method and variable/method access domain control
function fruit(name, color){// public variablethis.name = name;this.color = color;}
Use this to identify the public variable or method.
var apple = new fruit('apple', 'red');
Like most other languages, the new keyword is used to instantiate a class object. In this way, the value of apple. name is 'apple '. Internal variables use the var Keyword:
function book(){ var subject = "welcome to nowamagic.net"; }var b1 = new book();
In this case, access b1.subject and the result is "undefined ".
2. Class Extension
JavaScript is a dynamic language, so we can add attributes or methods to the class after it is created. The specific method is to use prototype:
function fruit(name, color){ // public variable this.name = name; this.color = color;}var apple = new fruit('apple', 'red');var orange = new fruit('orange', 'yellow');//apple.gender = 'undefined'fruit.prototype.gender=1;//apple.gender = 1//orange.gender = 1//fruit.prototype.gender = 1
The variables apple and orange all have the gender attribute and the value is 1.
Some people also call prototype extended content static methods or attributes. I think this is not suitable because static content cannot be accessed through objects, and if you change apple. gender, does not affect orange. gender value. Another reason is that gender cannot be accessed directly using a class name like static attributes in other languages:
apple.gender = 1 //error : student.gender undefined
You can only do this:
Fruit. prototype. gender // The value is 1.
3. Relationship between objects and Arrays
Var tom = {}; // typeof (tom) // objecttom ['email '] = 'qq @ qq.com'; // tom. the Email value is 'qq @ qq.com 'tom. website = "www.nowamagic.net"; // tom ["Website"] value: "www.nowamagic.net"
From this we can see that the field of the object can be accessed through arrays, and vice versa. Field access is more like an object-oriented style in terms of style, but it is very convenient to traverse objects using array orientation.
This article is available at http://www.nowamagic.net/librarys/veda/detail/246.