Learn Javascript Note 1 Javascript Object 1, javascript note
I have studied Js for a while before. Now I want to look at it again. It seems like new knowledge, indicating that I am not very good at Js (it is very bad, it has never been better, hey). Here, review
If you see it unfortunately, it's just your note.
The object-oriented idea in Js is absolutely very important. If you have read the code of great gods, you will know how important the object-oriented idea in Js is.
All things in JavaScript are objects: String, Number, Array, Date, and so on.
In JavaScript, objects are data with properties and methods.
Attributes and Methods
1. attributes are values related to objects. 2. The method is an action that can be executed on an object.
For example, cars are objects in real life.
Attributes of a car:
Car. name = Fiat car. model = 500 car. weight = 850 kg car. color = white
Car methods:
Car. start () car. drive () car. brake ()
In JavaScript, the object is data (variable) and has attributes and methods. When declaring a JavaScript variable:
var txt = new String("Hello World");Here, the String object has built-in properties and methods: txt. length (attribute) txt. indexOf ("World") (method)
Create a JavaScript Object (1) person = new Object ();
Person. firstname = "John ";
Person. lastname = "Doe ";
Person. age = 50;
Person. eyecolor = "blue"; (2)
Person = {firstname: "John", lastname: "Doe", age: 50, eyecolor: "blue "};
(3) Use the object constructor
function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; }
I have been studying javascript For a long time, but I have never systematically read a book. Which of the following can I recommend,
Javascript authoritative guide (version 6), which can be used as a desktop reference manual. If you have nothing to do, please refer to it.
Javascript advanced programming, repeated study, essential
The essence of javascript language, this book is very thin, but the word is a thousand Yuan, you must look at it, recommended it
For a typical javascript instance, you can see how the program is written in it. Learn it and recommend it.
Javascript mode, similar to the essence of the language, is also very thin, you have time to look at it, recommended
The other is to learn about the framework when javascript is learned. Let's take a look at jquery, node, and yui. After all, there are a lot of frameworks currently, and there are also sharp jquery basic tutorials (for the Turing version, both books are good. I wish you a good understanding of javascript and hope to adopt it
Question 1: Functions in javascript and current objects
1:
From your code,
B2 is called only within b1, and b3 has exceeded the b1 scope. B2 will be created only when b1 is called, b1 stops running, and b2 is destroyed immediately. To call an instance, you must write b1 as a class, create an instance through new, and then call it. Or
Function b1 (){
B2 = function (){};
}
Run b1 before calling b3.
B1 (); b3 (); so that b3 will not report the "b2 not function" error;
But the logic of the Code has also changed,
In short, b2 cannot be called in your code b3.
2:
This method is very strange. normal html elements cannot contain the stop loop method,
This can be called only when these two methods (stop and loop) are appended to obj. This Code uses the Framework Code. If you use the framework, you cannot bind events like this, is not in line with the JS logic, the Code must be written in this way to achieve:
Dd. look = function () {alert ('look ');}
Dd. stop = function () {alert ('stop ');}
Dd. onmouseover = function () {this. look ();}
Dd. onmouseout = function () {dd. stop ();}
If these two methods are appended to obj,
This. stop () and obj. stop () have the same effect, which is a writing problem. Some people prefer this.
3:
Prototype is an object type. attributes in prototype objects are shared by all instances, especially some class attributes. Sometimes (most of the time) You do not want each instance to share the same attributes, therefore, the variable values are rarely defined in prototype. The methods should be shared only when the methods are defined.
Can be abbreviated:
Obj. prototype = {
Constructor: obj,
Method 1: function (){},
Method 2: function (){}
......
};
Or you can study the with () statement to simplify the code, but it is better to use less (with will change the scope), which will confuse the readability of the Code.