Home  >  Article  >  Web Front-end  >  Summary of basic knowledge points of JavaScript

Summary of basic knowledge points of JavaScript

高洛峰
高洛峰Original
2017-01-21 09:44:431289browse

Variables defined outside the function must be global variables; for variables defined within the function, if var is declared, then the variable is a local variable. If var is not declared, then the variable is a global variable.

1. Global variables and local variables

JavaScript

var global = "Global";
test();
function test(){
  var local = "Local";
  document.writeln(global);
  document.writeln(local);
}
document.writeln(global);
document.writeln(local);

2. Two types of Cookies

i ) persistent cookie, which will be stored on the client's hard drive.

ii) Session cookie: It will not be stored on the client's hard drive, but will be placed in the memory of the browser process. When the browser is closed, the session cookie will be destroyed.

3. In JavaScript, a function is an object.

4. In JavaScript, there is no concept of method (function) overloading.

5. Function object

There is a Function object in JavaScript, and all custom functions are of Function object type. All parameters accepted by the Function object are of string type, the last parameter is the function body to be executed, and the previous parameters are the parameters that the function really needs to accept.

6. Implicit object arguments

In JavaScript, each function has an implicit object arguments, which represents the parameters actually passed to the function. arguments.length represents the number of parameters actually passed.

7. Function name.length

Each function object has a length attribute, indicating the number of parameters the function expects to accept. It is different from the arguments of a function. arguments.length indicates the number of parameters actually accepted by the function.

8. There are five primitive data types in JavaScript

Undefined, Null, Boolean, Number and String. (Note: In JavaScript, there is no char data type)

The Undefined data type has only one value: undefined;

The Null data type has only one value: null;

There are two values ​​of Boolean data type: true and false;

9. typeof operator

typeof is a unary operator, followed by the name of the variable, used to obtain the data type of the variable. There are 5 return values: undefined, boolean, number, string and object.

10. In JavaScript, if the function does not declare a return value, it will return undefined11. The relationship between null and undefined

undefined is actually derived from null. For example:

The relationship between null and undefined

JavaScript

alert(undefined == null);
//浏览器返回true

11. Forced type conversion

There are three types in JavaScript Casting: Boolean(value), Number(value), String(value).

12. Object object

In JavaScript, all objects are inherited from the Object object.

Object object

JavaScript

var object = new Object();
for(var v in object){
  alert(v);
}

In the above code, the browser does not print anything, which does not mean that the Object object does not have any Attributes. The following code tests whether the properties in the Object object can be enumerated. If false is returned, it means that the properties in the Object object cannot be enumerated.

The properties in the Object object cannot be enumerated

JavaScript

alert(object.propertyIsEnumerable("prototype"));

If the browser pops up a false dialog box, it means that the properties in the Object object It cannot be enumerated.

Next let’s see if the properties in the window object can be enumerated

The properties in the window object can be enumerated

JavaScript

for (var v in window) {
  console.log(v);
}

In the Chrome browser, we will see a lot of properties printed in the browser debugging console, indicating that the properties in the window object can be enumerated.

13. In JavaScript, you can dynamically add or delete the properties of an object

Dynamicly add/delete the properties of an object

JavaScript

var object = new Object();
alert(object.username);//undefined
  
object.username = "zhangsan";
alert(object.username);//zhangsan
  
object["password"] = "123";
alert(object.password);//123
  
delete object.username;//此时,username属性已经被删除
alert(object.username);

14. The most common way to define objects in JavaScript

The most common way to define objects

JavaScript

var object = {
  username:"zhangsan",
  password:12345
};
alert(object.username);
alert(object.password);

15. Array

Array definition

JavaScript

//方法一
var array = new Array();
array.push(1);
array.push(2);
array.push(3);
alert(array.length);
  
//方法二(推荐)
var array = [1,25,4];
array.sort();
alert(array);

Call the sort() method of the array, browse The processor prints 1, 25, 4, which is not what we expect.

For the sort method of JavaScript array, it will first convert the content to be sorted into a string (call the toString() method), and sort according to the order of the string.

The following method can get the results we expect (sorted by array size):

Array sorting

JavaScript

function compare(num1,num2) {
  var temp1 = parseInt(num1);
  var temp2 = parseInt(num2);
  if (temp1 < temp2) {
    return -1;
  } else if (temp1 == temp2) {
    return 0;
  } else {
    return 1;
  }
}
  
var array = [1,25,3];
array.sort(compare);
alert(array);

We then use anonymous functions to implement:

Anonymous function sorting

JavaScript

var array = [1,25,3];
  
array.sort(function(num1,num2){
  var temp1 = parseInt(num1);
  var temp2 = parseInt(num2);
  if (temp1 < temp2) {
    return -1;
  } else if(temp1 == temp2) {
    return 0;
  } else {
    return 1;
  }
});
  
alert(array);

16. 5 ways to define objects in JavaScript (JavaScript There is no concept of class, only objects) i) Expand its properties and methods based on existing objects

Expand its properties and methods based on existing objects

JavaScript

var object = new Object();
//添加name属性
object.name = "zhangsan";
//添加sayName方法
object.sayName = function(name) {
  this.name = name;
  alert(this.name);
};
object.sayName("kyle");//调用sayName方法,name属性被修改为kyle,浏览器将打印kyle

The simplest method is not convenient to use and is suitable for temporary needs of an object.

ii) Factory method to create objects

Factory method without parameters:

JavaScript

//工厂方法
function createObject() {
  var object = new Object();//创建一个对象
  object.name = "zhangsan";//为该对象添加一个name属性
  object.password = "123";//为该对象添加一个password属性
  object.get = function() {//为该对象添加一个get方法
    alert(this.name+","+this.password);
  };
  return object;//返回该对象
}
  
var object1 = createObject();//调用createObject工厂方法创建对象object1
var object2 = createObject();//调用createObject工厂方法创建对象object2
object1.get();//调用对象get方法
object2.get();//调用对象get方法

Factory method with parameters :

JavaScript

function createObject(name,password) {
  var object = new Object();
  object.name = name;
  object.password = password;
  object.get = function() {
    alert(this.name+","+this.password);
  };
  return object;
}
  
var object1 = createObject("zhangsan","123");
var object2 = createObject("lisi","456");
object1.get();
object2.get();

Disadvantages of the above two factory methods without parameters and with parameters:

Every time an object is created, it is created in the memory A get method wastes memory and affects performance. Our expectation is to create two different objects, their properties are different, but the methods are shared. So next we need to improve the createObject factory method.

Improved factory method:

JavaScript

function get(){
  alert(this.name+","+this.password);
}
  
function createObject(name,password) {
  var object = new Object();
  object.name = name;
  object.password = password;
  object.get = get;
  return object;
}
  
var object1 = createObject("zhangsan","123");
var object2 = createObject("lisi","456");
object1.get();
object2.get();

 将get方法定义在createObject函数外面,这样每创建一个对象,get方法都是共用的。让一个函数对象被多个对象所共享,而不是每一个对象都拥有一个函数对象。

iii)构造函数方式创建对象

不带参数的构造函数:

JavaScript

function Person(){
  //在执行第一行代码前,js引擎会为我们生成一个对象
  this.name = "zhangsan";
  this.password = "123";
  this.getInfo = function() {
    alert(this.name+","+this.password);
  };
  
  //此处有一个隐含的return语句,用于将之前生成的对象返回(也是跟工厂方式不一样的地方)
}
  
var p1 = new Person();
p1.getInfo();

带参数的构造函数

JavaScript

function Person(name,password) {
  this.name = name;
  this.password = password;
  this.getInfo = function() {
    alert(this.name+","+this.password);
  };
}
  
var p1 = new Person("zhangsan","123");
var p2 = new Person("lisi","456");
p1.getInfo();
p2.getInfo();

iv)原型(prototype)方式创建对象

prototype是Object对象里面的一个属性

prototype

JavaScript

function Person(){
  
}
Person.prototype.name = "zhangsan";
Person.prototype.password = "123";
Person.prototype.getInfo = function() {
  alert(this.name+","+this.password);
};
  
var p1 = new Person();
var p2 = new Person();
p1.name = "kyle";//对象生成之后再去改变属性
p1.getInfo();
p2.getInfo();

单纯地使用原型方式有两个问题:第一,你无法在构造函数中为属性赋初值,只能在对象生成之后再去改变属性值。

prototype

JavaScript

function Person(){
  
}
Person.prototype.name = new Array();
Person.prototype.password = "123";
Person.prototype.getInfo = function() {
  alert(this.name+","+this.password);
};
  
var p1 = new Person();
var p2 = new Person();
p1.name.push("zhangsan");
p1.name.push("lisi");
p1.password = "456";
p1.getInfo();
p2.getInfo()

浏览器将会打印:zhangsan,lisi,456 和 zhangsan,lisi,123.

如果使用原型方式创建对象,那么生成的所有对象会共享原型中的属性,这样一个对象改变了该属性也会反应到其他对象当中。所以单纯地使用原型方式是不行的,还需要结合其他方式。接下来我们会继续介绍。

使用原型+构造函数方式来定义对象

JavaScript

function Person() {
  this.name = new Array();
  this.password = "123";
}
Person.prototype.getInfo = function() {
  alert(this.name+","+this.password);
};
  
var p1 = new Person();
var p2 = new Person();
p1.name.push("zhangsan");
p2.name.push("lisi");
p1.getInfo();
p2.getInfo();

使用原型+构造函数方式来定义对象,对象之间的属性互不干扰,各个对象间共享同一个方法,这是一种比较好的方式。

v)动态原型方式

JavaScript

function Person(){
  this.name = "zhangsan";
  this.password = "123";
  if(typeof Person.flag == "undefined"){
    alert("invoked");
    Person.prototype.getInfo = function(){
      alert(this.name + "," + this.password);
    }
    Person.flag = true;
  }   
}
  
var p1 = new Person();
var p2 = new Person();
p1.getInfo();
p2.getInfo();

在动态原型方式中,在构造函数中通过标志量让所有对象共享一个方法,而每个对象拥有自己的属性。上面代码在第一次创建对象时,首先通过一个判断语句,看flag属性是否已经定义,若没有定义,则通过原型方式添加getInfo方法,然后将flag设置为true,那么当第二次创建对象时,if语句判断为假,跳过执行。这样就达到了我们所期望的结果,创建的对象属性是互不干扰的,而对象的方法是共享的。

17、JavaScript中对象的继承(5种方式)

第一种方式:对象冒充

冒充对象继承

JavaScript

//父类
function Parent(username) {
  this.username = username;
  this.sayHello = function() {
    alert(this.username);
  };
}
//子类
function Child(username,password){
  //下面三行代码是最关键的
  this.method = Parent;
  this.method(username);
  delete this.method;
  
  this.password = password;
  this.sayWorld = function() {
    alert(this.password);
  };
}
  
var p = new Parent("zhangsan");
var c = new Child("lisi","123");
  
p.sayHello();
c.sayHello();
c.sayWorld()

第二种方式:call()

继承的第二种实现方式,call方法方式,call方法是Function对象中定义的方法,因此我们定义的每个函数都拥有该方法。call方法的第一个参数会被传递给函数中的this,从第2个参数开始,逐一赋给函数中的参数。

call 继承父类

JavaScript

function test(str) {
  alert(this.name+","+str);
}
var object = new Object();
object.name = "zhangsan";
//test.call相当于调用了test函数
test.call(object,"html5war");//将object赋给了this

接下来我们用call方式实现对象的继承

JavaScript

//父类
function Parent(username){
  this.username = username;
  this.sayHello = function() {
    alert(this.username);
  };
}
//子类
function Child(username,password) {
  Parent.call(this,username);
  this.password = password;
  this.sayWorld = function() {
    alert(this.password);
  };
}
  
var p = new Parent("zhangsan");
var c = new Child("lisi","123");
p.sayHello();
c.sayHello();
c.sayWorld();

第三种方式:apply()

apply 继承父类

JavaScript

//父类
function Parent(username){
  this.username = username;
  this.sayHello = function(){
    alert(this.username);
  };
}
//子类
function Child(username,password){
  Parent.apply(this,new Array(username));
  this.password = password;
  this.sayWorld = function(){
    alert(this.password);
  };
}
  
var p = new Parent("zhangsan");
var c = new Child("lisi","123");
p.sayHello();
c.sayHello();
c.sayWorld();

apply方法与call方法很类似,apply方法也是定义在Function对象中的方法,因此我们定义的每个函数都拥有该方法。

apply方法与call方法有一个区别:Parent.apply(this,new Array(username));传递的第二个参数为一个数组,而call方法传递的是一些离散的数据参数。这两个方法并不能说谁好谁坏,要看具体使用场景。

第四种方式:原型链方式(无法给构造函数传递参数)

原型链继承

JavaScript

function Parent() {
  
}
Parent.prototype.hello = "hello";
Parent.prototype.sayHello = function() {
  alert(this.hello);
};
  
function Child() {
  
}
Child.prototype = new Parent();
  
Child.prototype.world = "world";
Child.prototype.sayWorld = function() {
  alert(this.world);
};
  
var c = new Child();
  
c.sayHello();
c.sayWorld();

单纯使用原型链方式的缺点:没有办法传递参数,只有等对象创建完之后再去修改。我们接下来结合其它的方式解决这个问题。

第五种方式:混合方式(推荐)

使用混合方式实现对象的继承

JavaScript

function Parent(hello) {
  this.hello = hello;
}
Parent.prototype.sayHello = function() {
  alert(this.hello);
}
  
function Child(hello,world) {
  Parent.call(this,hello);
  this.world = world;
}
Child.prototype = new Parent();
Child.prototype.sayWorld = function() {
  alert(this.world);
}
  
var c = new Child("hello","world");
c.sayHello();
c.sayWorld();

以上这篇JavaScript基础知识点归纳(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持PHP中文网。

更多JavaScript基础知识点归纳相关文章请关注PHP中文网!

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