Object-oriented What is the object? Something visible, tangible, specific Object Finding: Describing Object Finding Object: What are the characteristics of the object: characteristics and behavior Summarize what is the object Object: Something with characteristics and behavior, specific characteristics Object: Something with attributes and methods, specific characteristics No Objects Create Objects The Set of Unordered Attributes There are four things you do when you customize the creation of new objects 1. Create (apply for a free) space in memory to store new objects 2. Set this as the current object 3. Setting the value of the object's properties and methods 4. Return this object. Ordinary function, lowercase beginning: mainly used to call function person() { } Custom constructor, capitalized beginning: used primarily to create objects function Person () { }
Object. Attribute or Method - ------------- Create Object Attribute or Method. If this Attribute exists, it will be assigned from a new value. If not, it will be created.
Object ["attribute"] - ------------------------------------------------------------------------------------------------------------------------------------------------
javascript is a scripting language It's an interpretative language. Is an object-based language It is a weak language. It's a dynamic language: 1. Code only knows what is stored when it's in this position. If it's an object, there are attributes and methods of the object. If it's a variable, there are functions of the variable. 2. Objects have nothing. As long as you click, you can add attributes and methods to the object. Object, Attribute, Object, Method () ------- Object ["Attribute"], Object ["Method"] () A Set of Unordered Attributes--Objects
How to create objects 1. Creating objects through system functions var obj = new Object(); // Object's attribute object. Name = value; obj.name = Xiaosu; obj.age = 17; // Add method object. Name = function; obj.eat = function () { console.log("I like the Year of the Ox"); };
Create objects by literal quantities var o ={} ; //Empty objects, like constructs obj.name = "Xiao Bai"; obj.age = 10; obj.sayHi = function () { console.log("I am:"+this.name); }; obj.sayHi(); //Disadvantage: Outside the object //For a disposable object outside the object, the change value can only be changed inside the object, but cannot be passed on. //Optimized var o = { name : "Zhang San", sex : "true", study : function () { console.log(); return "like to study"; } }; console.log("One is called"+ o.name+"Gender is"+ o.sex+ o.study()); console.log(o.name); console.log(this); console.log(o.sex); o.study();
Create Objects by Customization function obj (name,age) { this.name = name; this.age = age; this.study = function () { console.log("like to study"); } this.study (); } var p1 = new obj ("Zhang San",3);
json The format should be enclosed in double quotation marks regardless of the value or key. It is out of order and can not be used. for loop k What is stored is json Name of object attribute(Character string),key Not that attributes are unavailable json["key"]But json[key] var json = { "name" : "xiao", "age" : "11", "sex" : "nan" }; json.height = 180; for (var k in json) { console.log(json.k); //Create attributes, three undefined; object name. Attribute creation, if any, is reproduced, if not purely create attributes console.log(k); // console.log(json[k]); // }
Math object
Instance objects: Objects instantiated by constructors (via new)
Static object: No need to create, directly out is an object, the method is called through the static object (Math)
Example method: new
Static method: Math.name, Array.isArray pointed out directly by the object
var random=Math.random(); random=random*10+10; floor=Math.floor(14.2);//Downward Forensics round=Math.round(15.22);//round around abs=Math.abs(-22);//Absolute value ceil=Math.ceil(random);//Upward Forensics random=parseInt(random);//Pay attention to random reception console.log(random); console.log(floor); console.log(ceil); console.log(round); console.log(abs);
var power= Math.pow(2,4);//pow is the y power of x console.log(power); var sqrt=Math.sqrt(9); console.log(sqrt); //square root
Date object
var dt = new Date (); console.log(dt); // Current Standard Time: Thu Sep 06 2018 00:07:16 GMT+0800 (China Standard Time) var dt = new Date ("2018-9-5"); //Pass the string type directly, showing the Date type (time type) console.log(dt); //Wed Sep 05 2018 00:00 GMT+0800 (China Standard Time) var dt = new Date (2018,9,4); console.log(dt); //Thu Oct 04 2018 00:00 GMT + 0800 (China Standard Time) var dt = new Date (2018/9/4); console.log(dt); //Thu Sep 01 1970 08:00 GMT+0800 (China Standard Time) var now = new Date () console.log(now.valueOf()); //Get the number of milliseconds from January 1, 1970 var dt =Date.now() console.log(dt) //Get the number of milliseconds from January 1, 1970 var dt = new Date (); console.log(dt.getTime ()); console.log(dt.getMilliseconds()); console.log(dt.getSeconds()); //Return to 0-59 console.log(dt.getMinutes()); //Return to 0-59 console.log(dt.getHours()); //Return to 0-23 console.log(dt.getDay()); //Return to Wednesday console.log(dt.getDate()); //Return to the day of the month console.log(dt.getMonth()); //Back to this month, starting at 0. console.log(dt.getFullYear()); //Return to the year of four //Formatting date: September 6, 2018, 8:27:40: var dt = new Date (); var year =dt.getFullYear(); var month = dt. getMonth()+1; //Month begins at 0 and must be added 1 var day = dt.getDate(); var hour = dt.getHours(); var minute = dt.getMinutes(); var second = dt.getSeconds();