1. Common methods For
example: We want the object to have not only attributes but also behavior. (Behavior is reflected in functions in the program)
(1) Add speak function, output I am a good person
(2) Add jisuan function, you can calculate the result from 1 + .. + 1000
(3) Modify the jisuan function, this method Can receive a number n, calculate the result from 1 + .. + n
(4) Add the add member function, you can calculate the sum of two numbers
bubuko.com, cloth button
1 function Person (name, age) {
2 // This is to use the actual parameters passed to initialize the properties.
3 this.name = name;
4 this.age = age
5 // output your own name, here this.show is a public function, the function name is show
6 this.show = function () {
7 document.writeln (" Name = "+ this.name);
8}
9
10 // Add jisuan function, you can calculate the result from 1 + .. + 1000
11 this.jisuan = function () {
12 var res = 0;
13 for (var i = 0; i <1000; i ++)
14 {
15 res + = i;
16}
17 return res;
18}
19
20 // Modify the jisuan function. This method can receive a number n and calculate the result from 1 + .. + n
21 this.jisuans = function (n) {
22 var res = 0;
23 for (var i = 0; i <= n; i ++)
24 {
25 res + = i;
26}
27 return res;
28}
29}
30
31 var p1 = new Person ("
宋江", 90); 32 p1.show ( );
33 var p2 = new Person ("
林冲", 72); 34 p2.show ();
35 document.writeln ("<br/> res =" + p1.jisuan ());
36 document.writeln (" <br/> res = "+ p1.jisuans (100));
37
bubuko.com, cloth button
2. There are three other ways to add methods to objects:
2.1 Method one
function class name ()
{
this. attribute;
}
var object name = new class name ();
function function name () {
// execute
}
object name. property name = function name; // this is equivalent to assigning a function to the object name. property name, this property object
Name. The attribute name represents a function.
Object name. Attribute name ();
specific case:
bubuko.com, cloth button
function Person () {
this.name = "abc";
this.age = 900;
}
function show1 () {
window.alert ("hello" + this.name);
}
// Create a p1 object
var p1 = new Person ();
// Put the show1 function to p1.abc
p1.abc = show1; // If it is written like this: p1.abc = show1 ( ), The calculation result of show1 () will be returned to p1.abc. If show1 () is currently called, undefine will be returned to p1.abc
p1.abc (); // Call
Note:
window.alert (p1.abc); // What will be output
window.alert (show1) // What will be output
bubuko.com, cloth button
bubuko.com, cloth button
p1.abc and show1 output are the constructor of show1 in the above figure
2.2 Method 2
object name. property name = function (parameter list) {
// code
};
call
object name. property name (actual Parameter);
specific case:
bubuko.com, cloth button
1 function Person () {
2 this.name = "abc";
3 this.age = 900;
4}
5 var p1 = new Person ();
6 p1.abc = function show1 () {
7 window.alert ("hello" + this.name);
8};
9 p1.abc ();
bubuko.com, cloth button
2.3 Method 3: The
previous methods have a problem, it is the exclusive function of each object code so that if many objects, it will affect the efficiency
, js designers, provides us with another method prototype: so that multiple objects can share the function code:
bubuko.com, boob buckle
1 function Dog () {
2}
3 // Use prototype [class] to bind a function to shout
4 Dog.prototype.shout = function () {
5 window.alert ('puppy');
6}
7 var dog1 = new Dog ();
8 dog1.shout ();
9 var dog2 = new Dog ();
10 dog2.shout (); // Here ok
bubuko.com, Bubu buckle
The principle of the code
bubuko.com, Bubu buckle
// The method to determine whether dog1 and dog2 call the same memory address
window.alert (dog1 .shout == dog2.shout);
The role of the = sign
(1) When both sides of == are strings, then the comparison content is equal
(2) When both sides of == are numbers, then the size of the comparison numbers is equal
(3) When == is an object or an object function, compare the addresses to see if they are equal.
2.4 Case Analysis
What can the analysis output?
Case 1:
bubuko.com, cloth button
1 function Person () {
2 this.name = "
abc1 "; 3 this.age = 900;
4}
5
6 function show1 () {
7 window.alert ("hello" + this.name); // here this is the window object, no window.name is assigned, and only hello is output when output.
8}
9 var p1 = new Person ();
10 p1.abc = show1;
11 show1 ();
bubuko.com, Bubukou
Case 2:
Bubuko.com, Bubuko
function Person () {
this.name = "
abc1 "; this.age = 900;
}
var name =" Beijing "; // This sentence is equivalent to window.name =" Beijing "function show1 () {
window.alert (" hello "+ this.name); / / This is the window object here
}
var p1 = new Person ();
p1.abc = show1;
window.show1 ();
// The following is not related to case two
function Person () {
this.name = "abc";
this.age = 900;
this.abc = function (v1,
window.alert (this.name + "" + this.age + "" + v1 + "" + v2);
}
}
var p1 = new Person ();
p1.abc ();
p1.abc ("Beijing", " Tianjin ");
var p2 = new Person ();
p2.abc ();
p2.abc (" Nanjing "," Tokyo ");
function Person () {
this.name =" abc ";
this.age = 900;
this.abc = function (v1, v2) {
window.alert (this.name + "" + this.age + "" + v1 + "" + v2);
}
}
var p1 = new Person ();
p1.name = "China"; // Dynamically add an attribute
p1.abc ("Beijing", "Tianjin");
var p2 = new Person ();
p2.abc ("Nanjing", "Tokyo");
bubuko.com, bubuko.com,
bubuko.com,
every time a new instance of a class is created, two public properties name, age and a public method abc () are created instead of sharing
bubuko.com, cloth button
1 function Dog () {
2}
3
4 var dog1 = new Dog ();
5 // Dynamic binding a function to the shout attribute
6 dog1.shout = function () {
7 window.alert ( 'Puppy');
8}
9
10 dog1.shout ();
11
12 var dog2 = new Dog ();
1314 dog2.shout (); // Report error
15
16 // Hope all objects share a certain function
bubuko.com, Bubu buckle
solution:
bubuko.com, Bubuko
function Dog () {
}
// Use prototype [class] to bind a function to shout
Dog.prototype.shout = function () {
window.alert ('Puppy');
}
var dog1 = new Dog ();
dog1.shout ();
var dog2 = new Dog ();
dog2.shout (); // here ok
bubuko.com, bubuko.com,
bubuko.com,
// determine whether the methods of dog1 and dog2 call the same memory address
window.alert (dog1.shout == dog2.shout);
// Extend
var dog3 = new Dog ();
var dog4 = new Dog ();
var dog5 = dog4;
window.alert ("dog3 == dog4" + (dog3 == dog4)); // determine whether the object is the same object
window.alert ( "dog5 == dog4" + (dog4 == dog5)); // Determine whether the object is the same object
bubuko.com, cloth button
3, Object class
3.1 Create Person instance
bubuko.com, cloth button
/ * function Person () {
}
var p1 = new Person ();
p1.name = "sp"; * /
// Preliminarily experience the Object class and create objects directly through Object.
var p1 = new Object ();
p1.name = "sp";
window.alert (p1.constructor);
bubuko.com, cloth button
var i1= new Number(10);
window.alert(i1.constructor);
bubuko.com,布布扣
var i2 =10;
window.alert(i2.constructor);
bubuko.com,布布扣
var i=new Number(10);
Number.prototype.add = function(a){
return this + a;
}
window.alert(i.add(10).add(30));
var b = 90;
window.alert(b.add(40));
var arr = new Array(3);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
for(var i=0;i<arr.length;i++){
document.writeln(arr[i] +" ");
}
//使用Array提供的方法,颠倒数据。
arr.reverse();
document.writeln("<br/>");
for(var i=0;i<arr.length;i++){
document.writeln (arr [i] + "& nbsp;");
}
bubuko.com, cloth button
3.2 Deepen your understanding of classes and objects
How to add methods to classes (how to add methods to all objects of a certain type)
bubuko.com , Bubu buckle
/ *
Please think about extending a find (val) method to the Array object of js.
When an Array object calls this method, if it can find val, it returns its subscript, otherwise it returns -1
* /
var arr = new Array (3);
arr [0] = "George";
arr [1] = "John";
arr [2] = "Thomas";
// Now let ’s see how to add a method find () to all Array objects val);
Array.prototype.find = function (val) {
// Traverse the array this
for (var i = 0; i <this.length; i ++) {
if (val == this [i] {
return i;
}
}
return -1;
}
document.writeln ("John subscript =" + arr.find ("John"));
bubuko.com, Bubu buckle
3.3 Member functions-details
3.3.1 The parameters of member functions can be multiple For
example: function function name (parameter 1 ...) {}
3.3.2 The member function can have a return value or not , But if there is, there can only be a maximum of
3.3.3 js function overload is not supported, the specific case is as follows:
bubuko.com, cloth button
1 function test (a, b, c) {
2 window.alert ( "hello");
3}
4
5 function test (a) {
6 window.alert (a);
7}
8
// Three functions of the same name, the latter function will cover the previous function, there is no concept of overloading in js , Will only execute the function marked in red below
9 function test (a, b) {
10 window.alert (a + "" + b);
11}
12 // test (23);
13 window.test (3, "hello ");
bubuko.com, Bubukou
14
15 // Conclusion: When js calls a function, it is called according to the function name. If there are multiple functions with the same name, the last function is recognized.
16
17 function abc () {
18 var s = 0;
19 for (var i = 0; i <arguments.length; i ++) {
20 s + = arguments [i]; // arguments [i] start with 1 instead of 0
21 }
22 return s;
23}
24 window.alert (abc (1,2));
25 window.alert (abc (7,8,9));
https://www.cnblogs.com/hanmlp52/category/1727703.html
https://www.cnblogs.com/hanmlp52/category/1727705.html
https://www.cnblogs.com/hanmlp52/category/1727708.html
https://www.cnblogs.com/hanmlp52/category/1727709.html
https://www.cnblogs.com/hanmlp52/category/1727712.html
https://www.cnblogs.com/hanmlp52/category/1727714.html
https://www.cnblogs.com/hanmlp52/category/1727716.html
https://www.cnblogs.com/hanmlp52/category/1727718.html
https://www.cnblogs.com/hanmlp52/category/1727720.html
https://www.cnblogs.com/hanmlp52/category/1727722.html
https://www.cnblogs.com/hanmlp52/category/1727724.html
https://www.cnblogs.com/hanmlp52/category/1727727.html
https://www.cnblogs.com/hanmlp52/category/1727729.html
https://www.cnblogs.com/hanmlp52/category/1727730.html
https://www.cnblogs.com/hanmlp52/category/1727733.html
https://www.cnblogs.com/hanmlp52/category/1727736.html
https://www.cnblogs.com/hanmlp52/category/1727737.html
https://www.cnblogs.com/hanmlp52/category/1727740.html
https://www.cnblogs.com/hanmlp52/category/1727743.html
https://www.cnblogs.com/hanmlp52/category/1727745.html
https://www.cnblogs.com/hanmlp52/category/1727748.html
https://www.cnblogs.com/hanmlp52/category/1727749.html
https://www.cnblogs.com/hanmlp52/category/1727751.html
https://www.cnblogs.com/hanmlp52/category/1727754.html
https://www.cnblogs.com/hanmlp52/category/1727756.html
https://www.cnblogs.com/hanmlp52/category/1727757.html
https://www.cnblogs.com/hanmlp52/category/1727761.html
https://www.cnblogs.com/hanmlp52/category/1727763.html
https://www.cnblogs.com/hanmlp52/category/1727765.html
https://www.cnblogs.com/hanmlp52/category/1727767.html
https://www.cnblogs.com/hanmlp52/category/1727769.html
https://www.cnblogs.com/hanmlp52/category/1727771.html
https://www.cnblogs.com/hanmlp52/category/1727774.html
https://www.cnblogs.com/hanmlp52/category/1727776.html
https://www.cnblogs.com/hanmlp52/category/1727778.html
https://www.cnblogs.com/hanmlp52/category/1727781.html
https://www.cnblogs.com/hanmlp52/category/1727783.html
https://www.cnblogs.com/hanmlp52/category/1727786.html
https://www.cnblogs.com/hanmlp52/category/1727787.html
https://www.cnblogs.com/hanmlp52/category/1727790.html
https://www.cnblogs.com/hanmlp52/category/1727793.html
https://www.cnblogs.com/hanmlp52/category/1727795.html
https://www.cnblogs.com/hanmlp52/category/1727797.html
https://www.cnblogs.com/hanmlp52/category/1727799.html
https://www.cnblogs.com/hanmlp52/category/1727801.html
https://www.cnblogs.com/hanmlp52/category/1727802.html
https://www.cnblogs.com/hanmlp52/category/1727806.html
https://www.cnblogs.com/hanmlp52/category/1727807.html
https://www.cnblogs.com/hanmlp52/category/1727808.html
https://www.cnblogs.com/hanmlp52/category/1727811.html
https://www.cnblogs.com/hanmlp52/category/1727813.html
https://www.cnblogs.com/hanmlp52/category/1727815.html
https://www.cnblogs.com/hanmlp52/category/1727817.html
https://www.cnblogs.com/hanmlp52/category/1727820.html
https://www.cnblogs.com/hanmlp52/category/1727821.html
https://www.cnblogs.com/hanmlp52/category/1727823.html
https://www.cnblogs.com/hanmlp52/category/1727825.html
https://www.cnblogs.com/hanmlp52/category/1727829.html
https://www.cnblogs.com/hanmlp52/category/1727830.html
https://www.cnblogs.com/hanmlp52/category/1727832.html
https://www.cnblogs.com/hanmlp52/category/1727834.html
https://www.cnblogs.com/hanmlp52/category/1727837.html
https://www.cnblogs.com/hanmlp52/category/1727838.html
https://www.cnblogs.com/hanmlp52/category/1727840.html
https://www.cnblogs.com/hanmlp52/category/1727842.html
https://www.cnblogs.com/hanmlp52/category/1727843.html
https://www.cnblogs.com/hanmlp52/category/1727845.html
https://www.cnblogs.com/hanmlp52/category/1727846.html
https://www.cnblogs.com/hanmlp52/category/1727848.html
https://www.cnblogs.com/hanmlp52/category/1727849.html
https://www.cnblogs.com/hanmlp52/category/1727850.html
https://www.cnblogs.com/hanmlp52/category/1727852.html
https://www.cnblogs.com/hanmlp52/category/1727853.html
https://www.cnblogs.com/hanmlp52/category/1727854.html
https://www.cnblogs.com/hanmlp52/category/1727856.html
https://www.cnblogs.com/hanmlp52/category/1727857.html
https://www.cnblogs.com/hanmlp52/category/1727858.html
https://www.cnblogs.com/hanmlp52/category/1727860.html
https://www.cnblogs.com/hanmlp52/category/1727861.html
https://www.cnblogs.com/hanmlp52/category/1727862.html
https://www.cnblogs.com/hanmlp52/category/1727864.html
https://www.cnblogs.com/hanmlp52/category/1727865.html
https://www.cnblogs.com/hanmlp52/category/1727866.html
https://www.cnblogs.com/hanmlp52/category/1727868.html
https://www.cnblogs.com/hanmlp52/category/1727869.html
https://www.cnblogs.com/hanmlp52/category/1727870.html
https://www.cnblogs.com/hanmlp52/category/1727872.html
https://www.cnblogs.com/hanmlp52/category/1727873.html
https://www.cnblogs.com/hanmlp52/category/1727875.html
https://www.cnblogs.com/hanmlp52/category/1727876.html
https://www.cnblogs.com/hanmlp52/category/1727877.html
https://www.cnblogs.com/hanmlp52/category/1727879.html
https://www.cnblogs.com/hanmlp52/category/1727880.html
https://www.cnblogs.com/hanmlp52/category/1727881.html
https://www.cnblogs.com/hanmlp52/category/1727883.html
https://www.cnblogs.com/hanmlp52/category/1727884.html
https://www.cnblogs.com/hanmlp52/category/1727885.html
https://www.cnblogs.com/hanmlp52/category/1727887.html
https://www.cnblogs.com/hanmlp52/category/1727888.html
https://www.cnblogs.com/hanmlp52/category/1727890.html
https://www.cnblogs.com/hanmlp52/category/1727891.html
https://www.cnblogs.com/hanmlp52/category/1727892.html
https://www.cnblogs.com/hanmlp52/category/1727894.html
https://www.cnblogs.com/hanmlp52/category/1727895.html
https://www.cnblogs.com/hanmlp52/category/1727897.html
https://www.cnblogs.com/hanmlp52/category/1727898.html
https://www.cnblogs.com/hanmlp52/category/1727899.html
https://www.cnblogs.com/hanmlp52/category/1727901.html
https://www.cnblogs.com/hanmlp52/category/1727902.html
https://www.cnblogs.com/hanmlp52/category/1727904.html
https://www.cnblogs.com/hanmlp52/category/1727905.html
https://www.cnblogs.com/hanmlp52/category/1727907.html
https://www.cnblogs.com/hanmlp52/category/1727908.html
https://www.cnblogs.com/hanmlp52/category/1727909.html
https://www.cnblogs.com/hanmlp52/category/1727911.html
https://www.cnblogs.com/hanmlp52/category/1727912.html
https://www.cnblogs.com/hanmlp52/category/1727913.html
https://www.cnblogs.com/hanmlp52/category/1727915.html
https://www.cnblogs.com/hanmlp52/category/1727916.html
https://www.cnblogs.com/hanmlp52/category/1727918.html
https://www.cnblogs.com/hanmlp52/category/1727919.html
https://www.cnblogs.com/hanmlp52/category/1727920.html
https://www.cnblogs.com/hanmlp52/category/1727922.html
https://www.cnblogs.com/hanmlp52/category/1727923.html
https://www.cnblogs.com/hanmlp52/category/1727925.html
https://www.cnblogs.com/hanmlp52/category/1727926.html
https://www.cnblogs.com/hanmlp52/category/1727927.html
https://www.cnblogs.com/hanmlp52/category/1727929.html
https://www.cnblogs.com/hanmlp52/category/1727930.html
https://www.cnblogs.com/hanmlp52/category/1727932.html
https://www.cnblogs.com/hanmlp52/category/1727933.html
https://www.cnblogs.com/hanmlp52/category/1727934.html
https://www.cnblogs.com/hanmlp52/category/1727936.html
https://www.cnblogs.com/hanmlp52/category/1727937.html
https://www.cnblogs.com/hanmlp52/category/1727940.html
https://www.cnblogs.com/hanmlp52/category/1727941.html
https://www.cnblogs.com/hanmlp52/category/1727942.html
https://www.cnblogs.com/hanmlp52/category/1727943.html
https://www.cnblogs.com/hanmlp52/category/1727945.html
https://www.cnblogs.com/hanmlp52/category/1727946.html
https://www.cnblogs.com/hanmlp52/category/1727948.html
https://www.cnblogs.com/hanmlp52/category/1727949.html
https://www.cnblogs.com/hanmlp52/category/1727951.html
https://www.cnblogs.com/hanmlp52/category/1727952.html
https://www.cnblogs.com/hanmlp52/category/1727953.html
https://www.cnblogs.com/hanmlp52/category/1727955.html
https://www.cnblogs.com/hanmlp52/category/1727956.html
https://www.cnblogs.com/hanmlp52/category/1727959.html
https://www.cnblogs.com/hanmlp52/category/1727960.html
https://www.cnblogs.com/hanmlp52/category/1727962.html
https://www.cnblogs.com/hanmlp52/category/1727963.html
https://www.cnblogs.com/hanmlp52/category/1727965.html
https://www.cnblogs.com/hanmlp52/category/1727966.html
https://www.cnblogs.com/hanmlp52/category/1727967.html
https://www.cnblogs.com/hanmlp52/category/1727969.html
https://www.cnblogs.com/hanmlp52/category/1727970.html
https://www.cnblogs.com/hanmlp52/category/1727971.html
https://www.cnblogs.com/hanmlp52/category/1727973.html
https://www.cnblogs.com/hanmlp52/category/1727974.html
https://www.cnblogs.com/hanmlp52/category/1727976.html
https://www.cnblogs.com/hanmlp52/category/1727977.html
https://www.cnblogs.com/hanmlp52/category/1727978.html
https://www.cnblogs.com/hanmlp52/category/1727980.html
https://www.cnblogs.com/hanmlp52/category/1727981.html
https://www.cnblogs.com/hanmlp52/category/1727982.html
https://www.cnblogs.com/hanmlp52/category/1727984.html
https://www.cnblogs.com/hanmlp52/category/1727985.html
https://www.cnblogs.com/hanmlp52/category/1727986.html
https://www.cnblogs.com/hanmlp52/category/1727988.html
https://www.cnblogs.com/hanmlp52/category/1727989.html
https://www.cnblogs.com/hanmlp52/category/1727991.html
https://www.cnblogs.com/hanmlp52/category/1727992.html
https://www.cnblogs.com/hanmlp52/category/1727993.html
https://www.cnblogs.com/hanmlp52/category/1727995.html
https://www.cnblogs.com/hanmlp52/category/1727996.html
https://www.cnblogs.com/hanmlp52/category/1727998.html
https://www.cnblogs.com/hanmlp52/category/1727999.html
https://www.cnblogs.com/hanmlp52/category/1728000.html
https://www.cnblogs.com/hanmlp52/category/1728002.html
https://www.cnblogs.com/hanmlp52/category/1728003.html
https://www.cnblogs.com/hanmlp52/category/1728005.html
https://www.cnblogs.com/hanmlp52/category/1728006.html
https://www.cnblogs.com/hanmlp52/category/1728007.html
https://www.cnblogs.com/hanmlp52/category/1728009.html
https://www.cnblogs.com/hanmlp52/category/1728010.html
https://www.cnblogs.com/hanmlp52/category/1728012.html
https://www.cnblogs.com/hanmlp52/category/1728013.html
https://www.cnblogs.com/hanmlp52/category/1728015.html
https://www.cnblogs.com/hanmlp52/category/1728016.html
https://www.cnblogs.com/hanmlp52/category/1728018.html
https://www.cnblogs.com/hanmlp52/category/1728019.html
https://www.cnblogs.com/hanmlp52/category/1728020.html
https://www.cnblogs.com/hanmlp52/category/1728022.html
https://www.cnblogs.com/hanmlp52/category/1728023.html
https://www.cnblogs.com/hanmlp52/category/1728025.html
https://www.cnblogs.com/hanmlp52/category/1728026.html
https://www.cnblogs.com/hanmlp52/category/1728027.html
https://www.cnblogs.com/hanmlp52/category/1728029.html
https://www.cnblogs.com/hanmlp52/category/1728030.html
https://www.cnblogs.com/hanmlp52/category/1728032.html