The Word object-oriented JavaScript is actually a bit superfluous, because the language of JavaScript is completely object-oriented and cannot be used in a non-object-oriented way. However, the common weakness of most novice programmers (including those using JavaScript) is to write code according to functionality, regardless of context or organization.
To fully understand how to write optimized JavaScript code, you must
1) Understand how JavaScript objects work
2) What is the difference between them and the other language objects?
3) and how to use it is good for you.
The part of the tear is finished, the bottom dry to the bird.
1. Objects and how objects are created
Objects are the basis of JavaScript. In fact, all the things in this language are objects. Most of the functionality of this language is based on this. An object is a collection of properties, similar to a hash table structure in other languages.
by build-in Object.var obj = new Object (); obj.val = 0;obj.click = function () {Console.log (' click me ... ');} by {}var obj = {val:0, click:function () {Console.log (' click me ... '); }}//by Constructorvar obj = function () {}var obj1 = new OBJ (); var obj2 = new Obj1.constructor ();
2. Public methods
Public methods are always accessible to end users in the context of an object. To implement a public method that can be used in every instance of an object, you must understand a property ' prototype ', which is described in detail later in the blog post.
Creates new Object.function User (name, age) {this.name = Name;this.age = age;} Adds public method for User.User.prototype.getName = function () {return this.name;} User.prototype.getAge = function () {return this.age;} TestVar user = new User (' Byorn '), Console.log (User.getname ()); Console.log (User.getage ());
3. Private methods
Private methods define some code that is only accessible internally to the object, and not externally accessible.
function User (name, age) {this.name = Name;this.age = Age;function showname () {console.log (name);} Call Private method//show Errorsshowname ();} var user = new User (' Byron ');//Cannot call this metod//would show error.user.showName ();
4. Privileged Methods
This method literally feels like a dick, where the hell is it?
Privileged methods (privileged method), which refer to those that are viewed and processed (in objects) as if they have variables, allowing the user to access them in a public way.
function User (name, age) {this.name = Name;this.age = Age;this.showname = function () {console.log (name);}} var user = new User (' Byron ');//We can get name of user by this way.user.showName ();/But we cannot access the Privat E variable ' name ' User.Name ()//Show error.
5. Static Methods
It's a little touch. Other object-oriented languages are easy to understand static methods. is the static method defined in the class.
The nature of a static method is no different from any other general function, the main difference being that other functions exist in the form of static attributes of the object. As attributes, they cannot be accessed in the context of the object's example, but only in that context of the object's book. (a bit of a mouthful, and see the following example will make you suddenly enlightened)
function User (name, age) {this.name = Name;this.age = age;} Static method. User.clone = function (user) {return new user () {user.getname,user.getage}}
6. Static Variables
For static methods, it is natural to think of static variables, and how to represent static variables in JavaScript. is divided into two situations
1) private static variables
Here is involved in the knowledge of closures, do not understand the self-brain repair. Directly on the code:
private static variable (function () { var privatestaticvar = ' private static variable '; Func = function () { This.setprivatestaticvar = function (value) { Privatestaticvar = value; } This.getprivatestaticvar = function () { return privatestaticvar; } } }) (); var func1 = new Func (); var func2 = new Func (); Console.log (Func1.getprivatestaticvar ()); Console.log (Func2.getprivatestaticvar ()); Func1.setprivatestaticvar (' private '); Console.log (Func1.getprivatestaticvar ()); Console.log (Func1.getprivatestaticvar ());
2) public static variables
This is a bit like a public static method, on the code:
Func = function () { this.test = ' Test '; } func.acfun= ' net '; Console.log (Func.acfun); Net
Basic JavaScript Object-oriented concepts and examples