Home  >  Article  >  Web Front-end  >  A summary of how to learn JavaScript

A summary of how to learn JavaScript

巴扎黑
巴扎黑Original
2017-08-04 13:25:381338browse

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 predefined object that comes with the system, and the other is the one written by the user himself. object. The general content is as shown below:


##

1: Core Object


##1.Array object

Array object, used to create arrays, use new to create


Commonly used methods As shown below:



##Common attributes :






Explain the creation and use of Array objects 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();//显示清空后数组的长度

Run result:




##Description:

prototype Used to add new properties and methods, the usage is: Array.prototype.functionName=functionName



##2, Function object

Function object can be used during program execution Dynamically generate functions and call functions.

Commonly used attributes are as follows:



# Commonly used methods are as follows:



The following examples illustrate:


<script language="javascript">
    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));  //调用函数并输出
</script>
Note: Function objects cannot be written as smaller functions. Function is an object, used to dynamically create functions. Function is a keyword. The function obtained through function new is also an instance of the Function object.

##3, String object

String object provides properties and methods for processing strings. When using, you can assign the value directly or create it through new and then assign the value

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>


这里主要想说明2cacc6d41bbb37262a98f745aa00fbf0为什么是分开的。

我们可以先看一下分开写的网页源码:





看不出什么不同,接下来我们看一下不分开时网页源码:



 

    结果很明显,如果不分开写2cacc6d41bbb37262a98f745aa00fbf0 会和前面的3f1c4e4b6b16bbbd69b2ee476dc4f83a配对,这样,浏览器会提示我们有误。


三:事件对象

event对象是一种只与事件处理过程有关的对象。

JavaScript中的事件跟vb中学过的事件一致,有鼠标事件,键盘事件,加载事件,等。。

事件的发生肯定有其出发者,触发发生后,这一事件也就随即结束。

事件在JavaScript中使用很简单,就是在关键字前加On。

The above is the detailed content of A summary of how to learn JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn