JavaScript mode Reading Notes Chapter 1 literal and constructor
1. Object literal volume-1. The custom object created in Javascript is variable in the task. You can add functions as needed from an empty object. The object literal mode allows us to add functions to an object when creating it.
<script>
// Define an empty object
var dog = {};
// Add an empty object
dog.name = "Liubo";
// Add method to object
dog.getName = function(){
return dog.name;
};
// Change attributes and Methods
dog.getName = function(){
returen "LaoLiu";
};
// Delete all attributes/methods
delete dog.name;
</script>
<script>
var dog = {
name: "LaoLiu",
getName:function(){
return this.name;
}
};
</script>
-2. Object literal syntax a. Wrap the object in braces. B. Separate attributes and methods by commas. C. Use a colon to separate the attribute name and attribute value. D. When the copy is changed, it will end with right brackets.
2. Custom constructor:
<script>
var Person = function(name){
this.name = name;
this.say = function(){
return "I am " + this.name;
}
}
var adam = new Person("Adam");
console.log(adam.say());
</script>
When the constructor is called using the new operator:
A. Create an empty object, and the this variable references this object. It also inherits the prototype of this function.
B. attributes and methods are added to the object referenced by this.
C. The newly created object is referenced by this and the result is implicitly returned.
The above code is executed in the background as follows:
var Person = function(name){
var this = {};
this.name = name;
this.say = function(){
return "I am " + this.name
};
//return this;
};
3. the return value of the constructor.
<script>
var Objectmark = function(){
this.name = "This is it!";
var that = {};
that.name = "And that is that!";
Return that; // The name initialized for the first time will be ignored because of return that.
};
var o = new Objectmark();
console.log(o.name);
</script>
4. Force the new Mode
<script>
function Man(){
this.name = "Lao Liu";
}
// Create a new object
var laoLiu = new Man();
console.log(typeof laoLiu);// object
console.log(laoLiu.name);//Lao Liu
// Reverse mode
// The new operation is not used.
var pangLiu = Man();
console.log(typeof pangLiu);//undefined
console.log(pangLiu.name);//Cannot read property 'name' of undefined
</script>
5, array, 1, var a = [3]; // declare an array with a length of 1 and a [0] = 3.
2, var a = new Array (3); // declare an Array with a length of 3
6. JSON: JSON parsing of key-value contained in braces:
<script>
var jstr = '{"mykey" : "my value"}';
// Reverse mode
var data = eval('(' + jstr + ')');
console.log(data);
// Recommended
data = JSON.parse(jstr);
console.log(data.mykey);
</script>
JSON encapsulation:
<script>
var dog = {
name : "LaoLIu",
dob: new Date(),
legs: [1, 2, 3, 4]
};
var jsonStr = JSON.stringify(dog);
console.log(jsonStr);//{"name":"LaoLIu","dob":"2014-11-07T06:17:28.097Z","legs":[1,2,3,4]}
</script>
7. Regular Expression a. Use new RegExp to generate a regular expression. B. Use the literal syntax
8. five basic types of the basic type package Javascript: Numbers, strings, Boolean, null, and undefined.
Corresponding to: Number () String () Boolean ()
<script>
var n = 100;
console.log(typeof n);//number
var nobj = new Number(100);
console.log(typeof nobj);//object
var s = "hello";
console.log(s.toUpperCase());//HELLO
var greet = "1 2 3 4";
console.log(greet.split(" ")[0]);//1
</script>
9. Error object (exception capture)
<script>
try{
throw{
Name: "MyError", // custom error type
message:'oops',
extra: 'This is Exception',
Remedy: genericErrorhandler // specify the function to handle this error
};
}catch(e){
alert(e.message);
E. remedy (); // call genericErrorhandler
}
function genericErrorhandler(){
alert("Hi, BigLiu!");
}
</script>