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:
The constructor ( method ) name is the same as the class name.
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 )
The constructor method name is the same as the class name
The primary role is to complete the initialization of the new object instance
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)