js函数调用的方式

1.作为函数调用

function my(a,b){
     return a*b;
}
var a=10,b=3,sum;
sum=my(a,b);
document.write(sum);

2.作为对象的方法调用
使用方式“对象.方法”
(1) 对象中定义的方法:

var people={
    name:"name",
    age:24,
    printpeople:function(){
        return this.name + this.age;
    }
}
document.write(people.printpeople());

(2)从prototype继承的方法

Array.prototype.myFunction=function(){
    for(i=0;i<this.length;i++){
        this[i]=this[i][0].toUpperCase()+
        this[i].substring(1,this[i].length);
	
	}
}
	var arr = ["benchi","dazhong"];
	arr.myFunction();
	document.write(arr);

3.使用构造函数调用函数
new即是使用构造函数的关键字。

function people(name,age){
		this.name = name;
		this.age = age;
		this.print=print;
		function print(){
			document.write(this.name + this.age);
		}
	}
	var person = new people("liming",24);
	person.print();

4.一个函数作为另一个函数(对象)的方法被函数(对象)调用
在JavaScript中,函数也是对象,函数有其属性和方法,call() 和 apply() 是预定义的函数方法。

function myFunction(a, b) {
    return a * b;
}
myObject = myFunction.call(myObject, 10, 2); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值