Variables defined outside the function must be global variables; variables defined inside 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 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 cookies will be stored on the client's hard disk.
ii) Reply cookie: It will not be stored on the client's hard disk, but will be placed in the memory of the browser process. When the browser is closed, the reply 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 parameter is the parameters that the function really needs to accept.
6. Implicit object arguments
In JavaScript, each function has an implicit object arguments, representing the parameters actually passed to the function. arguments.length represents the number of actually passed parameters.
7. Function name.length
Each function object has a length attribute, indicating the number of parameters that the function expects to accept. It is different from the arguments of functions. arguments.length represents 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)
There is only one value for the Undefined data type: undefined;
There is only one value for the Null data type: 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. It is used to obtain the data type of the variable. Its return values are: undefined, boolean, number, string and object.
10. In JavaScript, if the function does not declare the return value, it will return the relationship between undefined11, null and undefined
undefined is actually derived from null. For example:
The relationship between null and undefined
JavaScript
alert(undefined == null);//The browser returns true
11. Forced type conversion
There are 3 casts in JavaScript: Boolean(value), Number(value), String(value).
12. Object object
In JavaScript, all objects are inherited from Object objects.
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 carry 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.
Properties in Object objects 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 cannot be enumerated.
Next, let's see if the properties in the window object can be enumerated.
Properties in window objects are enumerable
JavaScript
for (var v in window) { console.log(v);}In Chrome, we will see a lot of properties printed out in the browser debugging console, indicating that the properties in the window object can be enumerated.
13. In JavaScript, you can dynamically add the attributes of an object or dynamically delete the attributes of an object.
Dynamically add/remove the properties of objects
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;//Undefined object.username = "zhangsan";
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
//Method 1 var array = new Array();array.push(1);array.push(2);array.push(3);alert(array.length); //Method 2 (recommended) var array = [1,25,4];array.sort();alert(array);
Call the sort() method of the array, and the browser prints 1, 25, 4, which is not the result we expect.
For the sort method of JavaScript array, it will first convert the content to be sorted into a string (called toString() method) and sort it in the order of the strings.
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 implement it in anonymous functions:
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. Five ways to define objects in JavaScript (there is no concept of classes in JavaScript, 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();//Add name attribute object.name = "zhangsan";//Add sayName method object.sayName = function(name) { this.name = name; alert(this.name);};object.sayName("kyle");//Call the sayName method, the name attribute is modified to kyle, and the browser will print kyleThe simplest way is not convenient to use and is suitable for temporarily requiring an object.
ii) Create objects in factory
Factory method without parameters:
JavaScript
//Factory method function createObject() { var object = new Object();//Create an object object.name = "zhangsan";//Add a name attribute object.password = "123";//Add a password attribute object.get = function() {//Add a get method alert(this.name+","+this.password); }; return object;//Return this object} var object1 = createObject();//Calendar a createObject factory method to create object1var object2 = createObject();//Call createObject factory method to create object object2object1.get();//Call the object get method object2.get();//Cell the object get method object2.get();//Cell the object get methodFactory 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, a get method is created in memory, which is a waste of memory and affects performance. And our expectation is to create two different objects whose properties are different, but the methods are shared. So next we need to improve the createObject factory method.
Improved factory approach:
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();Define the get method outside the createObject function, so that the get method is shared for each object created. Make a function object share by multiple objects, instead of each object having a function object.
iii) Constructor method to create an object
Constructor without parameters:
JavaScript
function Person(){ //Before executing the first line of code, the js engine will generate an object for us this.name = "zhangsan"; this.password = "123"; this.getInfo = function() { alert(this.name+","+this.password); }; //There is an implicit return statement here to return the previously generated object (which is also a different place from the factory method)} var p1 = new Person();p1.getInfo();Constructor with parameters
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 creation
prototype is a property in the Object 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";//After the object is generated, change the attribute p1.getInfo();p2.getInfo();There are two problems with simply using prototypes: First, you cannot assign initial values to attributes in the constructor, and you can only change the attribute values after the object is generated.
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()The browser will print: zhangsan, lisi, 456 and zhangsan, lisi, 123.
If an object is created using the prototype method, all generated objects will share the properties in the prototype, and if an object changes the property, it will also be reflected in other objects. Therefore, it is not possible to simply use the prototype method, and it also needs to be combined with other methods. We will continue to introduce it next.
Use prototype + constructor to define objects
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();Use the prototype + constructor method to define objects. The properties between objects do not interfere with each other, and each object shares the same method. This is a better way.
v) Dynamic prototype method
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();In dynamic prototype method, in the constructor, all objects share a method through flag quantities, and each object has its own attributes. When the above code creates an object for the first time, it first uses a judgment statement to see if the flag attribute has been defined. If it is not defined, add the getInfo method through the prototype, and then set the flag to true. Then when the object is created the second time, the if statement is judged to be false and execute is skipped. This achieves the desired result, the created object properties do not interfere with each other, and the object methods are shared.
17. Inheritance of objects in JavaScript (5 ways)
The first method: object impersonation
Imitate object inheritance
JavaScript
//Presiding class function Parent(username) { this.username = username; this.sayHello = function() { alert(this.username); };}//Subclass function Child(username,password){ //The following three lines of code are the most critical 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()The second method: call()
The second implementation method of inheritance, the call method, the call method is a method defined in the Function object, so each function we define has this method. The first parameter of the call method will be passed to this in the function, starting from the second parameter, and assigned to the parameters in the function one by one.
call inherits the parent class
JavaScript
function test(str) { alert(this.name+","+str);}var object = new Object();object.name = "zhangsan";//test.call is equivalent to calling the test function test.call(object,"html5war");//assign object to thisNext, we use the call method to implement object inheritance
JavaScript
//Presiding class function Parent(username){ this.username = username; this.sayHello = function() { alert(this.username); };}//Subclass 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();The third method: apply()
apply inherits the parent class
JavaScript
//Presiding class function Parent(username){ this.username = username; this.sayHello = function(){ alert(this.username); };}//Subclass 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();The apply method is very similar to the call method. The apply method is also a method defined in a Function object, so each function we define has this method.
There is a difference between the apply method and the call method: Parent.apply(this,new Array(username)); The second parameter passed is an array, while the call method passed some discrete data parameters. These two methods cannot be said to be good and bad, it depends on the specific usage scenario.
The fourth method: prototype chain method (cannot pass parameters to the constructor)
Prototype chain inheritance
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();Disadvantages of simply using prototype chain method: there is no way to pass parameters, you can only wait until the object is created before modifying it. Let’s solve this problem in combination with other ways.
The fifth method: mixed method (recommended)
Use hybrid methods to implement object inheritance
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();The above summary of basic JavaScript knowledge points (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.