JavaScript is a literal scripting language. It is a dynamically typed, weakly typed, prototype-based language with built-in support for types. Its interpreter is called the JavaScript engine, which is part of the browser and is widely used in client-side scripting languages. It was first used on HTML (an application under Standard Universal Markup Language) web pages to add dynamic functions to HTML web pages. .
JavaScript is the most popular scripting language on the Internet, present in all Web browsers around the world, and enhances user interaction with Web sites and Web applications. "Yan Shiba Javascript Advanced Tutorial" is an advanced tutorial for JavaScript learning, leading friends on how to expand their learning of the JavaScript language to adapt it to special needs.
Video playback address: http://www.php.cn/course/214.html
This course requires learning The usage of this, it is very important to understand the meaning of this,
1. this and the constructor
this itself is what needs to be used in the constructor when the class is defined, together with the constructor It couldn't be more natural.
/** * 页签 * * @class Tab * @param nav {string} 页签标题的class * @param content {string} 页面内容的class * */ function Tab(nav, content) { this.nav = nav this.content = content } Tab.prototype.getNav = function() { return this.nav; }; Tab.prototype.setNav = function(nav) { this.nav = nav; }; Tab.prototype.add = function() { };
According to JavaScript convention, this should be attached to attributes/fields, and methods should be placed on the prototype.
2. this and objects
Objects in JS can be created without classes. Some people may wonder that classes are templates for objects, and objects are copied from templates. How can we create them without classes? Create object? JS does, and you can write tens of thousands of lines of functional code without writing a single class. By the way, OOP talks about object-oriented programming, not class-oriented programming, right ^_^.
var tab = { nav: '', content: '', getNav: function() { return this.nav; }, setNav: function(n) { this.nav = n; } }
3. this and function
First of all, it makes no sense to put this and independent functions together. As mentioned before, this should be related to object-oriented. A pure function is just a low-level abstraction, encapsulation and reuse. Define showMsg as follows
function showMsg() { alert(this.message) } showMsg() // undefined
, and then call it as a function. This.message is undefined. Therefore, resolutely avoid using this in pure functions, but sometimes it is written like this. The calling method uses call/apply
function showMsg() { alert(this.message) } var m1 = { message: '输入的电话号码不正确' } var m2 = { message: '输入的身份证号不正确' } showMsg.call(m1) // '输入的电话号码不正确' showMsg.call(m2) // '输入的身份证号不正确'
. This method can save some code, such as when two classes/objects have a total of similarities. When using a method, you don’t have to write two copies, just define one and then bind it to the respective prototype and object. At this time, you are actually still using objects or classes (method 1/2), just indirectly.
The above is the detailed content of Yan Shiba Javascript advanced video material sharing. For more information, please follow other related articles on the PHP Chinese website!