但javascript中并没有类概念,所谓的类也是模拟而来,通过函数加闭包模拟出类成员及私有成员(关于闭包可以参见跨越边界: 闭包)。这里我们将用比较平实的方式来了解一下javascript中的”类”,避开一些生硬的原理。
既然是用function来模拟类,所以编写代码创建类的关键字还是function。我们创建一个座标点类。
<SPAN class=kwrd>function</SPAN> Point() {
<span class="kwrd">this</span>.X = 0;
<SPAN class=kwrd>this</SPAN>.Y = 0;
};
<span class="kwrd">var</span> zeroPoint = <span class="kwrd">new</span> Point();
alert(<SPAN class=str>"当前座标值( X:"</SPAN> + zeroPoint.X + <SPAN class=str>" , Y:"</SPAN> + zeroPoint.Y + <SPAN class=str>" )"</SPAN>);
大家都知道非静态类成员的访问需要通过对象来完成,所以先new出了一个Point类型对象,再通过该对象完成X和Y轴座标值的访问。从语法上来说,javascript类对象的创建过程和C#等差不多,但实现机制却不相同。这里的new创建出了一个Object,后续的Point()函数执行时将其this指向了这个新的Object对象。Point中的this.X和this.Y是Point类的两个公共成员,所以Point的对象可以直接对它们进行访问。
说到类成员或对象的成员,免不了要提到可访问性的问题。在javascript的类中,也有public成员和private成员之分,但究其细节却不尽相同。javascript私有成员也是些在类外部不可以通过对象进行操作的成员,其实在类的内部成员之间私有成员也不定能被访问。在类的内部一般只有私有成员和私有成员之间可以互相的访问,你可以认为其它成员权限不够不能操作这些隐私的东西,但如果你有特权,那就不一样了,管它私有公开照用不误。私有成员变量和普通变量声明一样,用var关键字,私有方法可以用var声明变量来接收方法对象,也可以像普通方法那样去构建。
<SPAN class=kwrd>function</SPAN> Lady() {
<span class="kwrd">var</span> age = 30;
<SPAN class=kwrd>var</SPAN> name = <SPAN class=str>"菜花"</SPAN>;
<SPAN class=kwrd>var</SPAN> think = <SPAN class=kwrd>function</SPAN>() {
alert(<span class="str">"其实我今年"</span> + age + <span class="str">"岁。"</span>);
};
<SPAN class=kwrd>function</SPAN> fancy(){
alert(<span class="str">"幻想变成20岁。"</span>);
};
<SPAN class=kwrd>this</SPAN>.Introduce = <SPAN class=kwrd>function</SPAN>() {
alert(<span class="str">"我叫"</span> + name + <span class="str">" , 今年20岁。"</span>);
};
};
<span class="kwrd">var</span> younglady = <span class="kwrd">new</span> Lady();
alert(younglady.age);<SPAN class=rem>//结果undefined</SPAN>
younglady.think(); <span class="rem">//不支持</span>
younglady.fancy(); <SPAN class=rem>//不支持</SPAN>
上面是一个Lady类,age、think、fancy都是私有成员,think和fancy方法可以访问age和name,think和fancy两个方法也可以互相进行调用。但它们是私有的,所以创建出来的youngLady对象并不能调用到age、think和fancy,当然也不能调用到name。如果私有成员只能互相之间调用,其实也就失去了私有成员存在的意义。javascript提供特权成员可以建立外界和私有成员互通的桥梁。特权成员是公共成员的一种,公共成员有普通公共成员、特权成员和对象公共成员。
特权成员就是在类中用this.XX的方式建立的成员,它们可以通过对象来被调用,它们也可以访问私有成员,可以建立私有成员被访问的通道。
<SPAN class=kwrd>function</SPAN> Lady() {
<span class="kwrd">var</span> age = 30;
<SPAN class=kwrd>this</SPAN>.Name = <SPAN class=str>"菜花"</SPAN>;
<SPAN class=kwrd>var</SPAN> think = <SPAN class=kwrd>function</SPAN>() {
alert(<span class="str">"其实我今年"</span> + age + <span class="str">"岁。"</span>);
};
<SPAN class=kwrd>function</SPAN> fancy() {
alert(<span class="str">"幻想变成20岁。"</span>);
};
<SPAN class=kwrd>this</SPAN>.Introduce = <SPAN class=kwrd>function</SPAN>() {
alert(<span class="str">"我叫"</span> + <span class="kwrd">this</span>.Name + <span class="str">" , 今年"</span> + age + <span class="str">"岁。"</span>);
};
};
<span class="kwrd">var</span> younglady = <span class="kwrd">new</span> Lady();
younglady.Introduce(); <SPAN class=rem>//Introduce</SPAN>
普通公共成员的创建,不在类的里面来编码,而是通过类的prototype来创建。添加普通公共成员都直接添加到类的prototype中,而prototype就是一个像JSON对象一样的成员集对象。当我们进行对象创建时,可以认为会将类prototype中的成员整体copy入新的Object对象中。
<SPAN class=kwrd>var</SPAN> younglady = <SPAN class=kwrd>new</SPAN> Lady();
younglady.Introduce(); <span class="rem">//Introduce</span>
Lady.prototype.Hobby = <span class="str">"上网"</span>;
Lady.prototype.GetName = <SPAN class=kwrd>function</SPAN>() {
<span class="kwrd">return</span> <span class="kwrd">this</span>.Name;
};
<SPAN class=kwrd>var</SPAN> lady2 = <SPAN class=kwrd>new</SPAN> Lady();
alert(lady2.GetName());
alert(lady2.Hobby);
上面代码通过prototype为Lady类添加了普通公共成员GetName方法和Hobby属性,因为是公共成员,所以它们可以和原先定意在类中的特权成员进行互相访问。因为公共成员可以互相访问。对上述代码做些修改。如下。
<SPAN class=kwrd>var</SPAN> younglady = <SPAN class=kwrd>new</SPAN> Lady();
Lady.prototype.Hobby = <SPAN class=str>"上网"</SPAN>;
Lady.prototype.GetName = <span class="kwrd">function</span>() {
<SPAN class=kwrd>return</SPAN> <SPAN class=kwrd>this</SPAN>.Name;
};
alert(younglady.GetName());
alert(younglady.Hobby);
先创建出Lady对象,再修改类成员,先前创建好的对象也具有了新的成员。这就是prototype做为类原型所带来的好处,这里简单理解,可以认为prototype是类对象的模版,模版的修改会影响到所有该类对象。
在添加普通成员的时候也可以来个批量的添加,直接用一个新的JSON对象来赋给prototype就可以了。但是要注意,现在是将原先的prototype进行了替换,在替换之前创建的对象引用的是旧的prototype对象,所以对prototype替换之前创建的对象不会有Hobby和GetName成员。
Lady.prototype = {
Hobby: <span class="str">"上网"</span>,
GetName: <SPAN class=kwrd>function</SPAN>() {
<span class="kwrd">return</span> <span class="kwrd">this</span>.Name;
}
};
<SPAN class=kwrd>var</SPAN> younglady = <SPAN class=kwrd>new</SPAN> Lady();
alert(younglady.GetName());
alert(younglady.Hobby);
除了在构建类时可以添加公共成员,还可以对对象直接进行成员操作。这在本小系列第二篇文章里有描述。这里做一下补充,对对象直接添加的成员,也是一种公共成员,这些成员也可以和类中原先具有的公共成员进行访问。
younglady.SetName = <SPAN class=kwrd>function</SPAN>(name) {
<span class="kwrd">this</span>.Name = name;
};
younglady.SetName(<span class="str">"菜明"</span>);
alert(younglady.GetName());
以上说了一下类成员的东西,下次再说说类继承相关的东西。(如有不当说法请指正)

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software