1.常用方法
比如:我们希望对象不但有属性,还希望他有行为。(行为在程序中要靠函数来体现)
(1) 添加speak函数,输出我是一个好人
(2) 添加jisuan函数,可以计算从1+..+1000的结果
(3) 修改jisuan函数,该方法可以接收一个数n,计算从1+..+n的结果
(4) 添加add成员函数,可以计算两个数的和
bubuko.com,布布扣
1 function Person(name,age){
2 //这个就是使用传入的实际参数,去初始化属性。
3 this.name = name;
4 this.age = age
5 //输出自己的名字,这里 this.show 就是一个公开的函数,函数名是show
6 this.show = function(){
7 document.writeln("名字=" + this.name);
8 }
9
10 //添加jisuan函数,可以计算从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 //修改jisuan函数,该方法可以接收一个数n,计算从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,布布扣
2.给对象添加方法还有其它三种方式:
2.1 方式一
function 类名()
{
this.属性;
}
var 对象名 = new 类名();
function 函数名(){
//执行
}
对象名.属性名 = 函数名;//这样就相当于把函数赋给 对象名.属性名,此时这个属性 对象
名.属性名就表示一个函数。
对象名.属性名();
具体案例:
bubuko.com,布布扣
function Person(){
this.name = "abc";
this.age =900;
}
function show1(){
window.alert("hello" + this.name);
}
//创建一个p1对象
var p1 = new Person();
//把show1函数,给p1.abc
p1.abc = show1; //如果是这样写:p1.abc = show1(),会把show1()的计算结果返回给p1.abc,当前要调用show1(),则返回undefine给p1.abc
p1.abc();//调用
注意:
window.alert(p1.abc); //会输出什么
window.alert(show1) //会输出什么
bubuko.com,布布扣
bubuko.com,布布扣
p1.abc和show1 输出的都是上图show1的构造函数
2.2 方式二
对象名.属性名 = function(参数列表){
//代码
};
调用
对象名.属性名(实际参数);
具体案例:
bubuko.com,布布扣
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,布布扣
2.3 方式三:
前面的几种方法有一个问题,那就每个对象独占函数代码,这样如果对象很多,则会影响效率
,js的设计者,给我们提供另一个方法 原型法:这样多个对象可以共享函数代码:
bubuko.com,布布扣
1 function Dog(){
2 }
3 //使用prototype[类]去绑定一个函数给shout
4 Dog.prototype.shout = function(){
5 window.alert(‘小狗‘);
6 }
7 var dog1 = new Dog();
8 dog1.shout();
9 var dog2 = new Dog();
10 dog2.shout(); //这里ok
bubuko.com,布布扣
对代码的原理说明
bubuko.com,布布扣
//判断dog1和dog2的方法是否调用同一个内存地址的方法
window.alert(dog1.shout == dog2.shout);
=号的作用
(1) 当 == 的两边都是字符串,则比较内容是否相等
(2) 当 == 的两边都是数字,则比较数的大小是否相等
(3) 当 == 是对象 或者是 对象函数,则比较地址是否相等
2.4 案例分析
分析能输出什么?
案例一:
bubuko.com,布布扣
1 function Person(){
2 this.name = "abc1";
3 this.age = 900;
4 }
5
6 function show1(){
7 window.alert("hello" + this.name); //这里的this 是表示window 对象,没有给window.name 赋值,输出的时候只输出hello。
8 }
9 var p1 =new Person();
10 p1.abc = show1;
11 show1();
bubuko.com,布布扣
案例二:
bubuko.com,布布扣
function Person(){
this.name = "abc1";
this.age = 900;
}
var name = "北京"; // 这句话等同于window.name = "北京"function show1(){
window.alert("hello" + this.name); //这里的this 是表示window 对象
}
var p1 = new Person();
p1.abc = show1;
window.show1();
//下面跟案例二没有关系
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.abc();
p1.abc("北京","天津");
var p2 = new Person();
p2.abc();
p2.abc("南京","东京");
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 = "中国"; //动态添加一个属性
p1.abc("北京","天津");
var p2 = new Person();
p2.abc("南京","东京");
bubuko.com,布布扣
bubuko.com,布布扣
每次new一个类的实例后,都会创建两个公共属性name,age和一个公共方法abc(),而不是共享
bubuko.com,布布扣
1 function Dog(){
2 }
3
4 var dog1 = new Dog();
5 //动态绑定一个函数给shout属性
6 dog1.shout = function(){
7 window.alert(‘小狗‘);
8 }
9
10 dog1.shout();
11
12 var dog2 = new Dog();
1314 dog2.shout();//这里报错
15
16 //希望所有的对象,共享某个函数
bubuko.com,布布扣
解决方案:
bubuko.com,布布扣
function Dog(){
}
//使用prototype[类]去绑定一个函数给shout
Dog.prototype.shout = function(){
window.alert(‘小狗‘);
}
var dog1 = new Dog();
dog1.shout();
var dog2 = new Dog();
dog2.shout(); //这里ok
bubuko.com,布布扣
bubuko.com,布布扣
//判断dog1和dog2的方法是否调用同一个内存地址的方法
window.alert(dog1.shout == dog2.shout);
//扩展
var dog3 = new Dog();
var dog4 = new Dog();
var dog5 = dog4;
window.alert("dog3==dog4" +(dog3==dog4)); //判断对象是否为同一个对象
window.alert("dog5==dog4" +(dog4==dog5)); //判断对象是否为同一个对象
bubuko.com,布布扣
3、Object 类
3.1 创建Person实例
bubuko.com,布布扣
/*function Person(){
}
var p1 = new Person();
p1.name ="sp";*/
//初步体验Object类,通过Object直接创建对象。
var p1 = new Object();
p1.name="sp";
window.alert(p1.constructor);
bubuko.com,布布扣
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] +" ");
}
bubuko.com,布布扣
3.2 加深对类和对象的认识
如何给类添加方法(如何给某类型的所有对象添加方法)
bubuko.com,布布扣
/*
请思考给js的Array对象扩展一个find(val)方法,
当一个Array对象调用该方法的时候,如果能找到val则返回其下标,否则返回-1
*/
var arr = new Array(3);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
//现在我们一起看看如何给所有Array对象添加一个方法find(val);
Array.prototype.find = function(val){
//遍历数组 this
for(var i=0;i<this.length;i++){
if(val == this[i]{
return i;
}
}
return -1;
}
document.writeln("John 下标 =" +arr.find("John"));
bubuko.com,布布扣
3.3 成员函数-细节
3.3.1 成员函数的参数可以是多个
比如:function 函数名(参数1...){}
3.3.2 成员函数可以有返回值,也可以没有,但是有的话,最大只能有一个
3.3.3 js中不支持函数的重载,具体案例如下:
bubuko.com,布布扣
1 function test(a,b,c){
2 window.alert("hello");
3 }
4
5 function test (a){
6 window.alert(a);
7 }
8
//三个同名的函数,后面的函数会覆盖前面的函数,js中没有重载的概念,只会执行下面标红的函数
9 function test(a,b){
10 window.alert(a + "" + b);
11 }
12 //test(23);
13 window.test(3,"hello");
bubuko.com,布布扣
14
15 //结论:js在调用一个函数的时候,是根据函数名来调用。如果有个多个函数名相同,则认最后那个函数。
16
17 function abc(){
18 var s=0;
19 for(var i=0;i<arguments.length;i++){
20 s+=arguments[i]; //arguments[i]的下标是从1开始的,而不是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