×

js 方法

如何封装常用的JS方法?js怎么调用方法

admin admin 发表于2023-10-20 10:16:53 浏览39 评论0

抢沙发发表评论

本文目录

如何封装常用的JS方法

1、JS封装就是尽量把使用的方式简单化,内部逻辑和使用解耦。通俗的说就是使用的时候只需要知道参数和返回值,其他条件尽量不要使用人员进行设置。2、JS封装的方法有函数方式、对象的方式、闭包的方式。举例123456789101112131415161718192021221)函数方式function kk(a,b){内部对a,b怎么处理就不需要关心了}2)对象方式function kk(a,b){this.x = a;this.y = b;}var k = new kk(1,2);//通过面向对象的方式alert(k.x);3)闭包方式function kk(a,b){var k = 1;return function tt(){k++;}}var u = kk(1,2);u();//闭包实现累加u();//闭包实现累加

js怎么调用方法

1:方法调用模式。请注意this此时指向myobject。/*方法调用模式*/var myobject={value:0,inc:function(){alert(this.value)}}myobject.inc()2:函数调用模式请注意this此时指向window/*函数调用模式*/var add=function(a,b){alert(this)//this被绑顶到windowreturn a+b;}var sum=add(3,4);alert(sum)3:构造器调用模式javascript语言精粹一书建议摒弃这中方式。因为有更好的方式。这里先不介绍。下次发表博文的时候贴出来。会在这里加一个连接。/*构造器调用模式 摒弃*/var quo=function(string){this.status=string;}quo.prototype.get_status=function(){return this.status;}var qq=new quo(“aaa“);alert(qq.get_status());4:apply调用模式==我们可以来看一个更有用的apply实例。看最下面的代码。/*apply*///注意使用了上面的sum函数//与myobject//这中调用方式的优点在于可以指向this指向的对象。//apply的第一个参数就是this指针要指向的对象var arr=;var sum=add.apply(myobject,arr);alert(sum);

js常用的方法有几种

concat()连接两个或更多的数组,并返回结果。join()把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。pop()删除并返回数组的最后一个元素push()向数组的末尾添加一个或更多元素,并返回新的长度。reverse()颠倒数组中元素的顺序。

JS的使用方法

1、基层处理:把表面浮灰、杂质、油污必须清理干净,对不平处和疏松、蜂窝、麻面部位要先用纳米硅抗渗堵漏剂或水泥修补平整。2、底涂层施工:JS防水乳胶兑2倍自来水充分搅拌均匀即为底涂料。涂刷时要用力来回搓至少3遍,确保与基层粘结良好,防止空鼓起皮。3、中涂与面涂施工:直接使用,不加水泥的情况下不能加水,分2~3道涂刷,每道施工间隔要等上一道干后才能施工下一道。对于转角处、女儿墙、穿墙管、排气孔、落水口等特殊部位,根据需要可用玻璃丝布作增强处理。4、潮湿基层施工:JS防水乳胶可在潮湿但无明水的基层上直接施工。如果施工场所通风不良影响干燥,或者地面太粗糙需增加涂层厚度,应配合水泥使用。5、防水层材料用量:底涂层用量约0.3~0.5kg/㎡,中涂层用量约1~3kg/ ㎡,面涂层用量约1~2kg/ ㎡;JS防水乳胶总用量一般为2.5~5kg/㎡,可根据工程需要自由增减,并可根据工程施工要求附加丙纶卷材、无纺布或玻璃丝布。

JS自带有几种方法

1 创建脚本块1: 《script language=”JavaScript”》2: JavaScript code goes here3: 《/script》 2 隐藏脚本代码1: 《script language=”JavaScript”》2: 《!--3: document.write(“Hello”);4: // --》5: 《/script》 在不支持JavaScript的浏览器中将不执行相关代码3 浏览器不支持的时候显示1: 《noscript》2: Hello to the non-JavaScript browser.3: 《/noscript》 4 链接外部脚本文件1: 《script language=”JavaScript” src=“http://img.sxsky.net/it//”filename.js“”》《/script》 5 注释脚本1: // This is a comment2: document.write(“Hello”); // This is a comment3: /*4: All of this5: is a comment6: */ 6 输出到浏览器1: document.write(“《strong》Hello《/strong》”); 7 定义变量1: var myVariable = “some value”; 8 字符串相加1: var myString = “String1” + “String2”; 9 字符串搜索1: 《script language=”JavaScript”》2: 《!--3: var myVariable = “Hello there”;4: var therePlace = myVariable.search(“there”);5: document.write(therePlace);6: // --》7: 《/script》 10 字符串替换1: thisVar.replace(“Monday”,”Friday”); 11 格式化字串1: 《script language=”JavaScript”》2: 《!--3: var myVariable = “Hello there”;4: document.write(myVariable.big() + “《br》”);5: document.write(myVariable.blink() + “《br》”);6: document.write(myVariable.bold() + “《br》”);7: document.write(myVariable.fixed() + “《br》”);8: document.write(myVariable.fontcolor(“red”) + “《br》”);9: document.write(myVariable.fontsize(“18pt”) + “《br》”);10: document.write(myVariable.italics() + “《br》”);11: document.write(myVariable.small() + “《br》”);12: document.write(myVariable.strike() + “《br》”);13: document.write(myVariable.sub() + “《br》”);14: document.write(myVariable.sup() + “《br》”);15: document.write(myVariable.toLowerCase() + “《br》”);16: document.write(myVariable.toUpperCase() + “《br》”);17: 18: var firstString = “My String”;19: var finalString = firstString.bold().toLowerCase().fontcolor(“red”);20: // --》21: 《/script》 12 创建数组1: 《script language=”JavaScript”》2: 《!--3: var myArray = new Array(5);4: myArray = “First Entry”;5: myArray = “Second Entry”;6: myArray = “Third Entry”;7: myArray = “Fourth Entry”;8: myArray = “Fifth Entry”;9: var anotherArray = new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”);10: // --》11: 《/script》 13 数组排序1: 《script language=”JavaScript”》2: 《!--3: var myArray = new Array(5);4: myArray = “z”;5: myArray = “c”;6: myArray = “d”;7: myArray = “a”;8: myArray = “q”;9: document.write(myArray.sort());10: // --》11: 《/script》 14 分割字符串1: 《script language=”JavaScript”》2: 《!--3: var myVariable = “a,b,c,d”;4: var stringArray = myVariable.split(“,”);5: document.write(stringArray);6: document.write(stringArray);7: document.write(stringArray);8: document.write(stringArray);9: // --》10: 《/script》 15 弹出警告信息1: 《script language=”JavaScript”》2: 《!--3: window.alert(“Hello”);4: // --》5: 《/script》 16 弹出确认框1: 《script language=”JavaScript”》2: 《!--3: var result = window.confirm(“Click OK to continue”);4: // --》5: 《/script》 17 定义函数1: 《script language=”JavaScript”》2: 《!--3: function multiple(number1,number2) { 4: var result = number1 * number2;5: return result;6: }7: // --》8: 《/script》 18 调用JS函数1: 《a href=”#” onClick=”functionName()”》Link text《/a》2: 《a href=“/”javascript:functionName“()”》Link text《/a》 19 在页面加载完成后执行函数1: 《body onLoad=”functionName();”》2: Body of the page3: 《/body》 20 条件判断1: 《script》2: 《!--3: var userChoice = window.confirm(“Choose OK or Cancel”);4: var result = (userChoice == true) ? “OK” : “Cancel”;5: document.write(result);6: // --》7: 《/script》 21 指定次数循环1: 《script》2: 《!--3: var myArray = new Array(3);4: myArray = “Item 0”;5: myArray = “Item 1”;6: myArray = “Item 2”;7: for (i = 0; i 《 myArray.length; i++) { 8: document.write(myArray.value);13: }14: }15: 16: //--》17: 《/SCRIPT》18: 《form name=’myForm’ onSubmit=’encryptForm(this); window.alert(this.myField.value);’》19: Enter Some Text: 《input type=text name=myField》《input type=submit》20: 《/form》 JavaScript就这么回事5:窗口和框架 54 改变浏览器状态栏文字提示1: 《script language=”JavaScript”》2: window.status = “A new status message”;3: 《/script》 55 弹出确认提示框1: 《script language=”JavaScript”》2: var userChoice = window.confirm(“Click OK or Cancel”);3: if (userChoice) { 4: document.write(“You chose OK”);5: } else { 6: document.write(“You chose Cancel”);7: }8: 《/script》 56 提示输入1: 《script language=”JavaScript”》2: var userName = window.prompt(“Please Enter Your Name”,”Enter Your Name Here”);3: document.write(“Your Name is “ + userName);4: 《/script》 57 打开一个新窗口1: //打开一个名称为myNewWindow的浏览器新窗口2: 《script language=”JavaScript”》3: window.open(“http://www.liu21st.com/”,”myNewWindow”);4: 《/script》 58 设置新窗口的大小1: 《script language=”JavaScript”》2: window.open(“http://www.liu21st.com/”,”myNewWindow”,’height=300,width=300’);3: 《/script》 59 设置新窗口的位置1: 《script language=”JavaScript”》2: window.open(“http://www.liu21st.com/”,”myNewWindow”,’height=300,width=300,left=200,screenX=200,top=100,screenY=100’);3: 《/script》 60 是否显示工具栏和滚动栏1: 《script language=”JavaScript”》2: window.open(“http: 61 是否可以缩放新窗口的大小1: 《script language=”JavaScript”》2: window.open(’http://www.liu21st.com/’ , ’myNewWindow’, ’resizable=yes’ );《/script》 62 加载一个新的文档到当前窗口1: 《a href=’#’ onClick=’document.location = ’125a.html’;’ 》Open New Document《/a》 63 设置页面的滚动位置1: 《script language=”JavaScript”》2: if (document.all) { //如果是IE浏览器则使用scrollTop属性3: document.body.scrollTop = 200;4: } else { //如果是NetScape浏览器则使用pageYOffset属性5: window.pageYOffset = 200;6: }《/script》 64 在IE中打开全屏窗口1: 《a href=’#’ onClick=”window.open(’http://www.juxta.com/’,’newWindow’,’fullScreen=yes’);”》Open a full-screen window《/a》 65 新窗口和父窗口的操作1: 《script language=”JavaScript”》2: //定义新窗口3: var newWindow = window.open(“128a.html”,”newWindow”);4: newWindow.close(); //在父窗口中关闭打开的新窗口5: 《/script》6: 在新窗口中关闭父窗口7: window.opener.close() 66 往新窗口中写内容1: 《script language=”JavaScript”》2: var newWindow = window.open(“”,”newWindow”);3: newWindow.document.open();4: newWindow.document.write(“This is a new window”);5: newWIndow.document.close();6: 《/script》 67 加载页面到框架页面1: 《frameset cols=”50%,*”》2: 《frame name=”frame1” src=“http://img.sxsky.net/it//”135a.html“”》3: 《frame name=”frame2” src=“http://img.sxsky.net/it//”about:blank“”》4: 《/frameset》5: 在frame1中加载frame2中的页面6: parent.frame2.document.location = “135b.html”; 68 在框架页面之间共享脚本如果在frame1中html文件中有个脚本1: function doAlert() { 2: window.alert(“Frame 1 is loaded”);3: } 那么在frame2中可以如此调用该方法1: 《body onLoad=”parent.frame1.doAlert();”》2: This is frame 2.3: 《/body》 69 数据公用可以在框架页面定义数据项,使得该数据可以被多个框架中的页面公用1: 《script language=”JavaScript”》2: var persistentVariable = “This is a persistent value”;3: 《/script》4: 《frameset cols=”50%,*”》5: 《frame name=”frame1” src=“http://img.sxsky.net/it//”138a.html“”》6: 《frame name=”frame2” src=“http://img.sxsky.net/it//”138b.html“”》7: 《/frameset》 这样在frame1和frame2中都可以使用变量persistentVariable 70 框架代码库根据以上的一些思路,我们可以使用一个隐藏的框架页面来作为整个框架集的代码库1: 《frameset cols=”0,50%,*”》2: 《frame name=”codeFrame” src=“http://img.sxsky.net/it//”140code.html“”》3: 《frame name=”frame1” src=“http://img.sxsky.net/it//”140a.html“”》4: 《frame name=”frame2” src=“http://img.sxsky.net/it//”140b.html“”》5: 《/frameset》

JS的初始化方法是

1、window.onload=function(){}

《script type=“text/javascript“》   

 window.onload=function(){          //初始化内容    }

《/script》

2、写初始化方法,页面顺序执行到初始化方法时初始化

《script type=“text/javascript“》   

 function init() {        // 初始化内容    };  

  init(); 

 《/script》

3、在body里面写初始化方法.

《body onload=’init()’》

《/body》

《script type=“text/javascript“》   

 function init(){         // 初始化内容           }

《/script》

扩展资料

js数组的初始化

方法一:

var myarray = new Array(66,80,90,77,59);

方法二:

var myarray = ;

方法三:

var myarray=new Array(5); 

myarray=66; 

myarray=80; 

myarray=90; 

myarray=77; 

myarray=59; 

数组的属性:

myarray.length; //获得数组长度

原生js方法怎么实现的

原生的JS函数或方法一般都是由运行环境提供,运行环境分多个。1、浏览器,一般浏览器都是用系统API写的,对WINDOWS,UNIX/LINUX系统是,C语言编写,相应的js原生方法,用C语言实现;特例:MAC OS 与IOS由OBJECTIVE-C,SWIFT提供系统API,相应的js原生方法用OBJECTIVE-C或SWIFT实现。2、adobe,C语言编写依赖操作系统API,js原生方法由C语言或OBJECTIVE-C,SWIFT现;看运行目标平台。3、node.js,mongodb,C语言实现,js原生方法由C语言实现。4、微软excel2010及以后版本,有可能C#或.net实现,具体不详,js原生方法由C#或.net实现。