Similar to Prototype.js learning_prototype
As a successful open source framework for JavaScript, Prototype.js encapsulates many useful functions. Although the official document does not provide any, a search on Google revealed a lot of related documents. However, I still encountered it in the process of learning to use it. I have come up with some questions. I hope familiar friends can give me more advice. I pay attention to these points when learning prototype.js. At the same time, I also talk about the learning results and problems encountered for each point. ^_^
1. Creation of classes
prototype.js has been encapsulated, this is very simple.
var Person=Class.create();
This creates a Person class, which must provide the implementation of the initialize method:
Person.prototype={
initialize:function(){
, can be customized as parameterized.
You can see that after defining a class in this way, it is clearly different from JavaScript's original method of defining a class through function. In this case, we can use Class.create to define the class. Use function to define functions directly.
Classes usually also involve the definition of static members (static in nature) and instance members (which need to be instantiated before they can be called).
This is also very easy in JavaScript:
Static members:
var Person={
name:'person',
getName:function(){return 'person' }
};
The Person.getName above can be called directly, but the eat method needs to be called through var person=new Person();person.eat();.
2. Class inheritance
Class inheritance is actually supported by JavaScript itself, but prototype provides another method.
According to the implementation supported by javascript:
var Student=Class.create();
Student.prototype=new Person();
In this way, Student inheritance to Person.
This can be achieved when using prototype:
var Student=Class.create();
Object.extend(Student.prototype,Person.prototype);
Subclasses need to When adding methods, you can use
Student.prototype.study=function(){};
or
Object.extend(Student.prototype,{
study:function(){}
} );
3. Event mechanism (monitoring and observation of class method execution)
I have encountered some doubts about the event mechanism. As an event mechanism, it mainly needs to provide the definition of events, the monitoring of events and Observation of events.
In JavaScript, events need to start with on, that is, as events, they need to be named like onclick:
Add an external event to the above Student, such as:
Student.prototype. study=function(){
This.onstudy();
}
Student.prototype.onstudy=function(){};
This onstudy is implemented by the corresponding instance, for example The example uses this method:
function studyThis(){
alert("study this");
}
var student=new Student();
student.onstudy=studyThis() ;
We usually want to monitor and observe events. Based on the bindAsEventListener and Observe provided by prototype, we tried this:
study.onstudy=watchStudy.bindAsEventListener(this);
function watchStudy(event){
alert("watch study");
}
According to the event mechanism, when executing study, you should be able to see two prompts: study this and watch study, but after the final execution, only I can see watch study prompts, why is this? According to the concept of listener, the original method should not be overwritten. However, I looked at the source code of prototype.js and found that the original method will indeed be overwritten according to the above writing method.
Observe tried this:
Event.observe(study,'study',watchStudy,false);
According to the observation mechanism, you should see two prompts when executing study, but After the final execution, this line had no effect at all.
Why is this?

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

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.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

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.

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.

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 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.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.
