JS oriented (based on) object programming--construction method (function)

Source: Internet
Author: User

Constructors ( methods ) Introduction

What is a constructor function? Before answering this question, let's take a look at a requirement: before we create a human object, we first create an object, then assign a value to his age and name attribute, and if I now ask that you specify the age and name of the object directly when creating a human object, what should I do?

A constructor ( method ) is a special method whose primary function is to complete the initialization of an object instance. It has several features:

    1. The constructor ( method ) name is the same as the class name.

    2. When you create an object instance, the system automatically calls the constructor of the class to complete the initialization of the new object.

constructor Basic usage :

function class name (argument list) {    attribute = parameter value;   do not take this as private, with this as common. }

The following example:

function Person (name, age) {    this. Name = name;  This refers to the current object (that is, the instantiated object)this    .  // When you create a person object, you can give the name and age directly var New Person ("abc", +); var New Person ("Hello", 9); Window.alert (p2.name);

Of course, you can also specify a function property when you initialize a property value to an object. The following example:

<! DOCTYPE html>functionJisuan (NUM1, num2, oper) {if(Oper = = "+") {                returnnum1+num2; } Else if(Oper = = "-") {                returnNum1-num2; } Else if(Oper = = "*") {                returnNum1*num2; } Else if(Oper = = "/") {                returnnum1/num2;            }        }        functionPerson (name, age, fun) { This. Name =name;  This. Age =Age ; This  . myfun =Fun  ; }        varP1 =NewPerson ("AA", 9, Jisuan); varP2 =NewPerson ("AA", 9,NULL); //Window.alert (p1.name);        //Window.alert (P1.myfun (), "+");Window.alert (P2.myfun (1, 2, "*"));//uncaught TypeError:p2.myFun is not a function</script>

Summary of construction methods ( functions )

    1. The constructor method name is the same as the class name

    2. The primary role is to complete the initialization of the new object instance

    3. When you create an object instance, the system automatically calls the object's construction method

The refinement of the class definition:

With the introduction of Constructors ( methods ) , the definition of our class ( prototype object) can be perfected in one step:

function class name () {    property;}

function class name () {    attribute;    function (method);  }

function class name (parameter 1, parameter 2, ...) {    = parameter 1;     = parameter 2;  }

Object-Oriented programming further understanding:

another form of creating an object :

1, for relatively simple objects, we can also create (you can specify normal properties and function properties), such as:

<! DOCTYPE html>var dog = {name: "Puppy", age                    :8,                     fun1:function() {Window.alert ("Hello, World "),                    fun2:function() {Window.alert (" OK ");}                   };         Window.alert (dog.constructor);         Window.alert (dog.name+dog.age);         Dog.fun1 ();         Dog.fun2 ();     </script>

2. Sometimes you see a method called:

The name of the function. Call (object instance);

Cases

 <! DOCTYPE html>var  dog={name: ' Hello ' };  function   Test () {Window.alert ( this         .name);        } test ();        Window.test ();         var  name = "Ahan" ;  // test.call (window);//Output Ahan  Span style= "Background-color: #ff0000;" >test.call (dog); //     <==> dog.test ();   </script>

3, for...in, such as:

<! DOCTYPE html>var dog = {name: ' nickname ',                   sayHello:function(A, b) {Window.alert ("result =" + (A +B));}                  };         // object Name [' property name ']        for (var in dog) {            Window.alert (Dog[key]);        }     </script>

Remember: You can use this form-the object name [' property name ']-to access the properties and methods in the object.

Another example:

<! DOCTYPE html>        Document.writeln ("* * * * Current browser Window object has properties and methods ****<br/ > ");          for (var in window) {            Document.writeln (key+ ":" +window[key]+ "<br>");        }    </script>

JS oriented (based on) object programming--construction method (function)

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.