首页 > 编程 > Java > 正文

javaWeb学习之js方法创建

2019-11-10 16:55:32
字体:
来源:转载
供稿:网友

javaWeb学习之js方法


js方法创建的几种方式:

方法一

最常用的方法

function Test(){ alert("test"); }

方法二

匿名函数

var test = function(){ alert("test"); }

方法三

这种就比较原型了

(function{ alert("test"); })()

方法四

在一个{}对象中存入一个方法,使用对象调用对象的方法。

var Test = { test:function(){ alert("test1"); } } var t = Test.test1();

方法五

使用JavaScript的原型定义一个方法。

var Test = function(){};//定义一个空函数 //复制一个Test的原型对象,增加两个方法。 Test.PRototype= { test1:function(){ alert("test1"); }, test2:function(){ alert("test2"); } } 使用: var t = new Test(); t.test2();
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表