The difference between JS object and JSON and this point problem

Source: Internet
Author: User

constructor function. The function to be called by the New keyword, which returns the object of the specified property.
Use the New keyword to invoke the function
New is an operator, and there are 4 things that happen when you call a function using the new operator:
(1) Create an empty object
(2) point the inside of the function to this empty object
(3) Execute the statement inside the order function
(4) Return this object
function Fun () {
Name: "Floret",
age:18,
Sex: "Female",
}
var xiaoming = new Fun ();
Console.log (xiaoming);
//fun {name: "Floret", age:18, Sex: "Female"}
object of JavaScript Basics
object is just a special data type with properties and methods
JavaScript has built-in objects such as String, Date, Array, and so on.
1: Create in a literal way, use {} As a defining symbol, with K-V to list all attributes
var obj = {
Name: "Floret",
age:18,
Sex: "Female",
sayhello:function () {
Console.log ("I am" + this.name);
}
}
An object is an unordered collection of attributes
2: Accessing the properties of an object
Objectname.propertyname
objectname["PropertyName"]
methods for accessing objects
Objectname.methodname ()
var obj = {
Name: "Floret",
age:18,
Sex: "Female",
sayhello:function () {
Console.log ("I am" + this.name);
}
}
Console.log (obj.name);
console.log (obj[' name ');
Console.log (Obj.sayhello);
console.log (obj[' SayHello ');
Console.log (Obj.sayhello ());
//Floret
//Floret
//function
//function
//I am a little flower
3: The relationship between the literal of the object and the JSON
JSON does not declare variables (there is no concept of variables in JSON) .
There is no semicolon {} After the end of JSON
JSON requires that all keys must have double quotation marks (no single quotes), and object literals do not require (double quotes must be added when the name of the key does not match the name of the identifier, English letters, numbers, _, $, not numbers).
4:this
Key Words
pointing to the object that called the function
and call related and define irrelevant
(1) when using () Call, point to Window
function MyFunction () {
Console.log (this);
}
MyFunction ();
//window {external:object, Chrome:object, Document:document, _async_start:1500782356654, _chrome_37_fix: Undefined ...}
(2) DOM element event handler, point to DOM object
function MyFunction () {
Console.log (this);
}
div.onclick=myfunction;
//div
(3) When the function is called by setinterval, settimeout, this is the Window object
setinterval (myfunction,2000);
(4) When a function is invoked as an object property, this is the object in this function.
var obj = {
Name: "Floret",
age:18,
Sex: "Female",
sayhello:function () {
Console.log ("I am" + this.name);
}
}
Obj.sayhello ();
(5) Call and apply method can arbitrarily set the function inside the this
function. Call (object);
function. Apply (object);
The function executes immediately, and this inside the function points to the object.
The difference is the transfer of parameters:
Sum.call (obj,1,2,3);//Call must list all parameters with commas
sum.apply (obj, [+]);//apply array to list all parameters
(6) The function called by new.
this refers to a new object created inside the system

The difference between JS object and JSON and this point problem

Related Article

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.