The object-oriented of JavaScript--Method _javascript Skills

Source: Internet
Author: User
Tags variable scope

JavaScript Object-oriented is a concept of fire in recent years, because the younger brother talents, although has done a lot of web projects, looked on the internet a lot of esoteric materials and tutorials, or their profound theory of knowledge, some time ago read a book, finally have their own understanding, today also come out to install a back, If you feel very esoteric, please directly despise me, if you feel wrong, please shoot bricks directly.

Get to know some of the following things first.

Writing function fn () {} or var fn=function () {} In JS code can be interpreted as objects, of course, arrays, and so on.

Before you understand object-oriented, learn a few things about the following.

1. Call to Object method

The function written at the outermost layer of JS can also be understood as a method of the Window object. A variable that is defined can also be called a property of a Window object. For example:

var test=function () {
alert ("123")
} of
course, you can also define the
function test () {
alert ("123")
}

As a Window object method we can call this

Test ()//Pop-up warning box 123 (because the Window object is a top-level object we can omit it)
Window.test ()//Pop-up warning box 123
window[' test ']//pop-up warning box 123, you can interpret test as a method value below the window array object.

Through the above example, we have a rough idea of how to use and invoke the object's methods.

2, Private method

A private method is a method that can be used only within the scope of an object. You can use variable scope to understand this thing.
The function of the example above can be understood as a private method of the Window object. Keep looking at the example below.

var pet=function () {
 function Showpet () {
 alert ("123")
 }
 Showpet ()//private method can be used within a function scope.
 var temp= "Private variable can access"
}
only within the scope of a function or object Showpet ()//Error
Pet.showpet ()
//Cannot call Var penguin=new pet ()//Instantiate a Pet object
Penguin.showpet ()// I'm sorry this is still not allowed to call you.

What if I want to define a method that can be invoked by the object's extraterritorial surface? How do I use the private method? Let's take a look at the next piece of content.

3. Static method

With the above question we continue with the above example.

var pet=function () {
 function Showpet () {//Private method
 alert ("123")
 }
 Showpet ()//private method can be used within a function scope.
}
Pet.show=function () {//To add a static method to the Pet object.
alert ("456")
}
Pet.name= "Penguin"//Adds a static property to the Pet object.
pet.show ()//Pop-up warning box 456
alert (pet.name)//Pop-up warning box Penguin
//Continue thinking collision
=====================
var penguin=new pet ()//Instantiate a Pet object
penguin.show ()//Sorry, this static method does not seem to be inherited by the instance. This kind of thinking is worthy of praise Ah, come on!

The above example shows you what is called a static method, of course you may not understand, in fact, I do not understand, because I am also a rookie, but as long as you see how to write a static method for the object, how to call the static method can be, perhaps one day, you suddenly understand will come back to teach me. With the above question, let's take a look at the method that the instantiated object can invoke.

4. Public methods

Public methods are usually implemented by modifying the stereotype of a constructor, and after modifying the prototype of an object, all instances of the object inherit the modification of the prototype (this sentence is extremely loaded B, if you feel blurred, please ignore it).

To modify the method of the object prototype, continue with the example above.

Pet.prototype.setname=function (str) {//Add a public method by modifying the stereotype to add the name name=str that modifies the instance object
;
}

See Example:

var pet=function () {
 function showname () {//Private Method
 alert (this.name)
 }
 this.show=function () {//If not understood here , please note that this method is introduced below.
 showname ();
 }
Pet.prototype.setname=function (str) {
name=str;
}
var penguin=new pet ()
 penguin.setname ("Penguin");//Add instance's name value to Penguin
 penguin.show ();   Pop-up Penguin
 penguin.setname ("Wind");//Add instance's name value to Wind
 penguin.show ();   Pop wind

Run the code to play.

<script>
var pet=function () {
 name: "123",
 function ShowName () {//Private Method
 alert (this.name)
 }
 this.show=function () {
 showname ();
 }
 }
Pet.prototype.setname=function (str) {
 name=str;
}
 var penguin=new pet ()
 penguin.setname ("Penguin");
Penguin.show ();
Penguin.setname ("Wind");
Penguin.show ();
</script>

5. Privileged method (object or function external interface)

In fact, we have used this method in the above example. This method can be invoked by the instantiated object inheritance. A method defined by the THSI keyword within the constructor. Privileged methods can be publicly accessed outside the constructor (instantiated objects only). It is also possible to access private members and methods, so the interface used as an object or constructor is the most appropriate, with privileged functions we can control the access of public methods to private methods, which have many applications in the expansion of the JS framework.

You reader can when the above is a section of P, we specifically to see how to define a privileged method, how to refer to a privileged method, continue to invoke the above example to see.

var pet=function () {
 function showname () {//Private Method
 alert (this.name)
 }
 this.show=function () {// Define a privileged method by using the This keyword.
 showname ();  Access to private methods in privileged methods;
 }
pet.prototype.setname=function (str) {
name=str;
}
var penguin=new pet ();//Instantiate a Pet object
 penguin.setname ("Penguin");//Invoke public method to modify
 penguin.show ();   Invoke the privileged method to access the private method, popup name

First, a privileged method is established by using This.fn=function () {} in the constructor. Accessing a private method in a privileged function;

An instantiated object can use a partial private method by accessing a privileged function, accessing the method of a privileged function with access to the public function.

The first part of the temporary understanding here, the next section will be an example to explain how the object is loaded B.

The above is the entire content of this article, I hope to help you, interested friends can see the "JavaScript Object-oriented-encapsulation" Thank you for the cloud Habitat Community support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.