JS Basic Learning

Source: Internet
Author: User
Tags object object script tag hasownproperty

JS Video:
53: If the return statement behind the function does not follow any value is equivalent to return a undefined,
If return is not written in the function, undefined is also returned.
56: Execute function immediately: function definition is executed immediately.
function () {
Alert ("I am an anonymous function");
}//This is a function object, but without a name, in order for her to execute immediately, just () enclose the anonymous function, make it a whole, and then () is equivalent to calling

(function () {
Alert ("I am an anonymous function");
})//This is equivalent to the function object () in the form of a call,

Why do anonymous functions need parentheses?
is because without parentheses, {alert ("I am an anonymous function") is added;} is treated as a block of code, and the function () in front of it is superfluous and will be an error. The parentheses are as a whole, as a function object, just like the one below.
var a=function () {
Alert ("I am an anonymous function");
}//A is a function object, and if you want to execute it, just a ()
You can also pass parameters to it:
(function (A, b) {
Console.log (a);
Console.log (b);
}) (123,456)//Give a pass 123 to B pass 456

58: Declaration of variables in advance:
Variables declared with the VAR keyword are declared (but not assigned) before all code executes, with the following two equivalents:
Console.log ("a=" +a);//Result: a=undefined
var a=123
That
var A;
Console.log ("a=" +a);//Result: a=undefined
a=123;
The above equivalent, if it becomes:
Console.log ("a=" +a);//Result: Will error. Because a has not yet been declared.
a=123;
58: Declaration of functions in advance:
A. Function name () {} Created with function declaration form
It is created before all code executes, so you can call the function before the function definition appears.
Fun ();//Call in front, define after, no error, will print out haha
function Fun () {
Console.log ("haha");
}
B. A function created with a function expression is not advanced, so it cannot be called before the function definition.
Fun2 ();//Error
var fun2=function () {
Console.log (' haha ');
}
There are two types of scopes in 58:js:
A. Global scope:
-JS code written directly in the script tag, all in the global scope
-Global scopes are created when a browser page is opened and destroyed when the page is closed
-There is a global object in the Global scope window, which represents a browser window created by the browser that can be used directly
-In the global scope,
The created variables are saved as properties of the Window object
The created function will be saved as a method of the Window object
-Variables in the global scope are global variables that can be accessed from any part of the page.
B. Function scope:
-The function scope is created when the function is called, and the function scope is destroyed after the function is executed.
-Each time a function is called, a new function scope is created, and they are independent
-The function scope can be accessed globally, and vice versa.
var c=33;
function Fun () {
Console.log ("c =" +c);
var c=10;
}
Fun ();//output c = Undefined, because the function scope has, so the variable declaration in advance. Does not take the global.
59:a. A variable declared in a function that does not use VAR is a global variable.
var c=33;
function Fun () {
c=10;
d=100;
}
Fun ();
Console.log ("c =" +c);//c also becomes global, C = 10
Console.log ("D =" +d);//d is global, D =100
B. Defining a parameter is equivalent to declaring a variable in a function scope.
var e=23;
function Fun (e) {
Console.log (e);
}
Fun ();//will show undefined because it is equivalent to having var e in the function;
60: Give two examples:
1) var a=123;
function Fun () {
alert (a);
a=456;
}
Fun ();//Call function, get 123
Alert (a)//456
2) var a=123;
function Fun (a) {//1 more than one formal parameter is equivalent to declaring a variable var a in a function
alert (a);
a=456;
}
Fun ();//Call function, get undefined, because a has declared
Alert (a)//123. Since 456 changed is a function scope declaration
3) alert (c);//error, because C is not declared,
C=true;
61: The parser is the browser when calling the function, each time the function is passed into an implicit parameter, this parameter is this
This points to different objects depending on how the function is called
A. When called as a function, this is always window
var name= "global";
function Fun () {
Console.log (this.name);
}
Fun ();//called as a function, so this is window, and Window.name is global, so: global

B. When invoked as a method, this is the object that invokes the method
function Fun () {
Console.log (this.name);
}
var obj={
Name: "Haha",
Sayhello:fun
}
Console.log (Obj.sayhello==fun)//true
Obj.sayhello ();//haha,
C. When called as a constructor, this is the newly created object
62:d. Be aware of the following:
var name= "global";
function Fun () {
Console.log (name);//Note that this is no longer this.name.
}
Fun ();//Global
var obj={
Name: "Haha",
Sayhello:fun
}
Obj.sayhello ();//Here is the point. Result: Globally, because name is not found in the function, it is searched globally, so it is global. It's not this.name.
63: Use Factory mode to create a large number of object core ideas:
The common parts of the object to be created are extracted, and then the different parts are built by passing in different parameters.
function Createpeople (name, age) {
var obj=new Object ();
Obj.name=name;
Obj.age=age;
Obj.sayname=function () {
alert (this.name);
}
return obj;
}
var obj1=createpeople ("Monkey King", 14);
var obj2=createpeople ("Pig", 18);
However, objects created with the factory method use a constructor that is object, the new object (), which makes it impossible to distinguish between many different types of objects
64: To solve the above problem, you can create a constructor that is designed to create a type of object, such as the person type, the dog type
--the constructor is a normal function, and there is no difference between the creation method and the normal function.
The difference is that the constructor is used to capitalize the first letter, similar to the class name in Java
--the difference between a constructor and a normal function is the method of invocation
The normal function is to call fun directly ();
The constructor needs to be called with the New keyword: new fun (), which is equivalent to Java constructs an object, uses the class name
A. Constructors
function person () {}
var per=new person ();
Console.log (per);//object Object
B. Common functions
function person () {}
var Per=person ();
Console.log (per);//undefined, because there is no return value
The execution flow of the constructor:
1) Create a new object immediately,//similar to var obj=new object ();
2) Set the new object to this in the function, you can use this in the constructor to refer to the new object//var per=new person (), per is the new object
3) Execute the code in the function
4) Returns the newly created object as a return value
function person () {
This.name= "Monkey King"//Here's this is per
This.sayname=function () {
Alert ("Hello MyName is:" +this.name);
}
}
var per=new person ();
Console.log (per.name);//Monkey King
65: Now our approach is created inside the constructor, that is, the constructor creates a new Sayname method every time it executes
Executing 10,000 times creates 10,000 new methods, and 10,000 methods are the same. You can make all objects share one.
Workaround 1: Define the method in global scope
function person () {
This.name= "Monkey King"//Here's this is per
This.sayname=fun;
}
The function is extracted to the global, but will pollute the global namespace, that is, everyone name conflict caused by the overwrite, see the subsequent prototype.
function Fun () {
Alert ("Hello MyName is:" +this.name);
}
66: Prototype prototype
A. Every function we create, whether normal or constructed, will add an attribute to the function prototype
This property corresponds to an object, that is, the prototype object
B. If the function calls prototype as a normal function, it has no effect.
C. When called as a constructor, it creates an object that has an implied property pointing to the constructor's prototype object
This property can be accessed through __proto__//var per =new person (), which is the implied object in per
namely: Console.log (Per.__proto__==person.prototype)//true
D. The prototype is equivalent to a common area, and all instances of the same class can be accessed to this prototype object//per, Per1,per2 can
E. You can set the common content in the object to the prototype object, so as to avoid the global pollution
F. When we access a property or method of an object, we look for it in the object itself, and if there is one, it is used directly, not to the prototype object.
Example:
function person () {
This.name= "Monkey King"
}
Person.prototype.sayname=function () {
alert (this.name);
}
var per=new person ();
Per.sayname ();///Monkey King
G. When you do not want to use the variables or methods in the prototype, direct global coverage can be, because he will first find themselves.
H. When you create a constructor in the future, you can uniformly add properties and methods that are common to these objects to the constructor's prototype object, so that you don't have to add each object separately.
Also does not affect the global scope, so that each object has a.

You can take a closer look at this episode of video.
67:1) use in to check if an object contains a property, if it is not in the object but there is a prototype, it will return true,//, so it is not accurate
2) You can use the object's hasOwnProperty () to check that the object itself contains the property.
function MyClass () {

}
Myclass.prototype.name= "Monkey King";
var MC =new MyClass ();
Console.log ("name" in MC);//true
Console.log (Mc.hasownproperty ("name"));//false
Console.log (Mc.hasownproperty ("hasOwnProperty"));//false, because there is no such method in itself, will find a prototype
Console.log (Mc.__proto__.hasownproperty ("hasOwnProperty"));//false, found no such method in the prototype.
Console.log (Mc.__proto__.__proto__.hasownproperty ("hasOwnProperty"))//true, in the prototype, will go to the prototype of the prototype to find
Console.log (mc.__proto__.__proto__);//[object Object]
Console.log (mc.__proto__.__proto__.__proto__);//null, head.

That is, when we use a property or method of an object, we find it in ourselves, and if we do, we use it directly;
If not then go to the prototype object, if the prototype object is used;
If it still does not, it will go to the prototype prototype to find, until the object is found;
There is no prototype for the object's prototype, and if a property or method is still not found in object, it is returned undefined
In general, the use of level two proto, namely Console.log (mc.__proto__.__proto__);
68: When we print an object directly in the page, the actual output is the return value of the object's ToString () method.
If we want to output an object without Outputting [Object Object], you can add a ToString () method to the object.
var per = new Person ("Monkey King", 18, "male");
Console.log (per);//[object Object]
Per.tostring=function () {///But this only modifies this object, and the other object remains [Object Object]
return "haha";
}
Console.log (per);//haha
So you can do this, add to the prototype object
Person.prototype.tostring=function () {
return "haha";
}
Or:
Per.__proto__.tostring=function () {
return "haha";
}
Each object that you create is ready for use.
69: Garbage collection: var obj=new Object ();
Obj=null;//js will automatically be garbage collected. This object is not referenced, so you can change the reference to null for objects that are not used
70:JS objects Three: Inner object, host object, custom object
The W3school offline manual has an introduction to the built-in objects.
Always add an element to the last position of the array, regardless of the index value: arr[arr.length]=70;
arr[arr.length]=80;
The arr[arr.length]=90;//is not indexed, and is added directly at the end.
71:

JS Basic Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.