The basic data types javascript to create a function, call the function in three ways, three ways to define objects, classes and inheritance to create a standard wording

The basic data types javascript

javascript is weakly typed scripting language, without specifying the data type of a variable when you declare variables.
javascript There are five basic data types. They are:

1. Value Type: integer or floating-point number comprising.
2. Boolean: only two true and false values.
3. string types: variable must be enclosed in quotation marks, marks may be single quotes may be a double quotes.
4. undefined type: designed to determine a initial value can be created, but no variables.
5. null Type: is used to indicate a variable is empty.

Value type:

<script type="text/javascript">
   var a = 3.12e1;
   var b = 45.0;
   var c = .34e4;
   var d = .24e-2;
</script>

String type:

 <script type="text/javascript">
	var a = "Hello java";
	var b = 'Hello php';
</script>

indexOf () position of the target string searchString appear for search.

lastIndexOf () position last search target occurrences.

<script type="text/javascript">
	var a = "hellojavascript";
	var b = a.indexOf("llo");
	var c = a.indexOf("llo",3);
	var d = a.lastIndexOf("a");
	alert(b + "\n" + c + "\n" + d);
</script>

substring () from the start (including) index, the cut end (not including) index, the character is not taken at the end of the index.
slice () and substring () function basically the same, the difference is acceptable as a negative index, negative values as an index, the index is calculated from the start to the right string.

<script type="text/javascript">
	var a = "hellojavascript";
	var b = a.slice(0,4);
	var c = a.slice(2,4);
	var d = a.slice(4);
	var e = a.slice(-3,-1);
</script>

Boolean type:
boolean values are only two: true and false

<script type="text/javascript">
	if(navigator.cookieEnabled){
	alert("浏览器支持使用cookie");
}
else{
	alert("浏览器禁用cookie");
}
</script>

undefined和null:

<script type="text/javascript">
	var x,y=mull;
	if(x==undefined){
		alert("声明变量后值默认为undefined");
}
if(x==null){
	alert("声明变量后值默认为null");
}
if(x==y){
	alert("x(undefined)==y(null)");
}
if(String.xyz === undefined){
	alert("不存在的属性值默认为undefined");
}
</script>

Create a function in three ways

1. The definition of a named function:
define a named function syntax is:

function functionName(parameter-list){
	statements
}

Examples;

<script type="text/javascript">
	hello('yeeku');
	function hello(name){
	alert(name + ",你好");
}
</script>

2. The definition of an anonymous function:
define a named function syntax is:

function(parameter-list){
	statements
}

Example:

<script type="text/javascript">
	var f = function(){
	document.writeln('匿名函数<br>');
	document.writeln('你好'+name);
};
f('yeeku');
</script>

3. Use Function class anonymous functions:

<script type="text/javascript">
		var f = new Function('name',"document.writeln('Function定义的函数<br>');"
			+ "document.writeln('你好' + name);");
		f('yeeku');
	</script>

Calling the function in three ways:

Direct call function Direct object as a function of additional caller, passing parameters in parentheses after the function call the function
To call () method call function To invoke a dynamic function, need to use call () method is invoked
To apply () method call function And call () method is similar to the basic function, can dynamic function calls

1. direct call function:

<script type="text/javascript">
	//调用window对象的alert()方法
	window.alert("测试");
	//调用p对象的walk方法
	p.walk()	
</script>

2. In call () method to call
the caller. Function (parameter 1, parameter 2 ...) = function .call (caller, parameter 1, parameter 2)

<script type="text/javascript">
		var each = function(array,fn){
			for(var index in array){
				fn.call(each,index,array[index]);
			}
		}
		each([4,20,3],function(index,ele){
			document.write("第"+index+"个元素是:"+ele+"<br>");
		});
	</script>

3. to apply () method call function:

<script type="text/javascript">
		var myfun = function(a,b){
	alert("a的值是:"+a+"b的值是:"+b);
}
	myfun.call(window,12,33);
	myfun.apply(window,[12,33]);
	var example = function(num1,num2){
	myfun.apply(this,arguments);
}
example(20,40);
	</script>

Three custom object syntax:

Create Object
Call the constructor with the new keyword defined objects
Object class definition objects using
Create an object using JSON syntax

1. Use the new keyword to call the object constructor is defined:

<script type="text/javascript">
	function Person(name.age){
		this.name = name;
		this.age = age;
    }	
    var p1 = new Person();
    var p2 = new Person('yeeku',29);
    document.writeln(p1.name + p1.age+"<br>");
    document.writeln(p2.name + p2.age+"<br>");
</script>

2. Use Object definition object classes:

<script type="text/javascript">
	var myObj = new Object();
	myObj.name = 'yeeku';
	myObj.age = 29;
	document.writeln(p1.name + p1.age+"<br>");
</script>

3. Using JSON syntax to create objects:

<script type="text/javascript">
	person = {
		name : 'yeeku',
		gender: 'male',
		son:{
			name:'nono',
			gender:1
		},
		info : function(){
			document.writeln(this.name + this.age);
		}
	}
</script>

Create a class and standard wording inheritance:

When defined javascript function, it will give the same name as a class, and the class is the constructor function

Here we achieve the creation of a class by example

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-=Type" Content="text/html;charset = utf-8" />
	<title>swutch and ifelse 语句</title>
	<script type="text/javascript">
		//定义一个Person类
		function Person(name,age){
			this.name = name;
			this.age = age;
			this.info = function(){
				document.writeln("姓名:" + this.name);
				document.writeln("年龄:" + this.age);
			}
		}
		var p1 = new Person('yeeku',29);
		p1.info();
		Person.prototype.walk = function(){
			document.writeln(this.name + '  abcd...0<br>');
		}
		document.writeln("<hr />");
		var p2 = new Person('leegang',30);
		p2.info();
		document.writeln("<hr />");
		p2.walk();
		document.writeln("<hr />");
		p1.walk();
	</script>
</head>
<body>
</body>
</html>

Inherited standard wording:

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-=Type" Content="text/html;charset = utf-8" />
	<title>swutch and ifelse 语句</title>
	<script type="text/javascript">
	    function Person(name,age){
			this.name = name;
			this.age = age;
		}
		//通过prototype方法为Person类定义一个sayHello函数
		Person.prototype.sayHello = function(){
			console.log(this.name+"向您打招呼!");
		}
		var per = new Person("牛魔王",22);
		per.sayHello();
		function Student(name,age,grade){
			//继承Person类
			Person.call(this,name,age);
			this.grade = grade;
		}
		//继承Person.prototype方法
		Student.prototype = new Person();
		//通过prototype方法为Student类定义一个intro函数
		Student.prototype.intro = function(){
			console.log("%s是个学生,读%d年纪",this.name,this.grade);
		}
		var stu = new Student("孙悟空",5);
		//stu调用Person类的方法,实现继承
		stu.sayHello();
		//stu调用自己的方法
		stu.intro();
	</script>
</head>
<body>
</body>
</html>

Guess you like

Origin blog.csdn.net/fight252/article/details/89363017