Home > Article > Web Front-end > JavaScript object learning summary_javascript skills
JavaScript Object
JavaScript provides multiple built-in objects, such as String, Date, Array, etc.
Objects are just special data types with properties and methods.
In JavaScript, almost every js used is inseparable from its objects. Let's take a deeper look at js objects.
The classification of objects in js is the same as the classification of functions in the language we have learned before. One is the system's own predefined object, and the other is the user's own written object called custom object.
The general content is as shown below:
1: Core Objects
1.Array object
Array object, used to create arrays, use new to create
The commonly used methods are as follows:
Common attributes:
The creation and use of Array objects are explained through examples, as follows.
<script language="javascript"> var data=new Array("1","2","3");//创建数组data document.writeln("data的初始内容为:"+data+","); //输出数组内容 function fun(){ //自定义函数fun()显示数组长度 if(this.length!=0){ document.writeln("<PRE>此数组的长度为:"+this.length+""); }else{ document.writeln("
此数组的长度为0"); } } Array.prototype.fun=fun; //为Array对象添加fun()方法 data.fun(); //调用fun()方法 data=new Array();//清空data数组内容 data.fun();//显示清空后数组的长度
运行结果:
说明:
prototype用于添加新的属性和方法,用法为:Array.prototype.functionName=functionName
2,Function对象
Function对象可以在程序执行的过程中动态生成函数并调用函数。
常用属性如下:
常用方法如下:
下面实例说明:
1a24b17f85e15c83fc29b7760e40bfcb
funSum=new Function("x","y","z=x+y;return z"); //创建函数funSum()
x=prompt("请输X值:","");
y=prompt("请输Y值:","");
x=Number(x);
y=Number(y);
alert(newfun(x,y)); //调用函数并输出
2cacc6d41bbb37262a98f745aa00fbf0
注意:Function对象不可写成小些的function。Function是对象,用于动态创建函数,function是关键字,通过function new出来的函数也是Function对象的实例。
3,String对象
String对象提供了对字符串进行处理的属性和方法。使用时可以直接赋值也可以通过new创建后再赋值
String对象有很多方法,主要是用户有关字符串在Web页面中的显示格式,字体大小,字体颜色,字符的搜索以及字符的大小写转换方面的功能。
通过以上对象的prototype属性,我们也看到了JavaScript的特点,它不仅提供给我们一些功能,还提供给用户自己添加功能的方法,简直是太好了。
二:文档对象
document对象,接触JavaScript的第一个例子是通过ID名称获取控件的名称,里面就用到document,当时没有多想,就当是关键字过了。
document对象是window对象的属性,也是唯一的即属于BOM又属于DOM的对象。
说到window对象,我们首先来看一下window中各对象间的关系。如下图:
了解了大体结构关系,接下看看看document对象的常用属性:
有关document对象的方法,这里说说使用最频繁的.write()/.writeln()方法。这两个方法的作用都是打印字符,不同的是writeln打印完后多了个换行。
下面写一个用document对象的write方法动态添加JavaScript文件的实例
<script language="javascript"> document.write("<script type=\"text/javascript\" src=\"textuser.js\">"+"</scr"+"ipt>"); </script>
Here I mainly want to explain why 2cacc6d41bbb37262a98f745aa00fbf0 is separated.
We can first take a look at the source code of the web page written separately:
I can’t see any difference. Next, let’s take a look at the source code of the web page when they are not separated:
The result is obvious. If we do not write 2cacc6d41bbb37262a98f745aa00fbf0 separately, it will be paired with the previous 3f1c4e4b6b16bbbd69b2ee476dc4f83a. In this way, the browser will prompt us that there is an error.
3: Event object
The event object is an object related only to the event processing process.
The events in JavaScript are consistent with the events learned in VB, including mouse events, keyboard events, loading events, etc. .
The event must have its originator. Once the trigger occurs, the event will end immediately.
Using events in JavaScript is very simple, just add On before the keyword.
The above article shares with you a summary of JavaScript object learning. If you want to know more, please continue to pay attention to this site.