


1.Object class
In JS, Object is the base class of all classes. When using the Object class to create a custom object, you do not need to define a constructor (constructor, prototype, hasOwnProperty(property))
var per = new Object(); per.name = 'zhangsan'; per.age = ; alert(per.name + per.age);
We want to get an object variable in the program, as long as it can store a large amount of data. At this time, we can consider using the Object class. The Object class avoids the definition of a constructor. Another commonly used property under the Object class: hasOwnProperty
var per = new Object(); per.name = 'zhangsan'; per.age = ; if per.hasOwnProperty('email'){ alert('具有email'); }else{ alert('无email'); }
2. Static attributes
In some object-oriented languages, you can use the static keyword to define static properties or static methods of a class, which can be simulated in JS.
Syntax:
Class name.Attribute name
Class name.Attribute=function(){}
function Person(){ } Person.count = ; var p = new Person(); Person.count++; var p = new Person(); Person.count++; var p = new Person(); Person.count++; alert(Person.count);
Add static properties and static methods:
function Person(){ Person.count++; //静态属性 Person.getCount=function(){ //静态方法 alert('当前共有' + Person.count + '个人'); } } Person.count = ; var p = new Person(); var p = new Person(); var p = new Person(); Person.getCount();
3. Closure
Concept: The so-called closure refers to an expression (usually a function) that has many variables and an environment bound to these variables, so these variables are also part of the expression.
Ask a question:
function display(){ var i=; } display(); //在这里,想访问局部变量i
In the global world, local variable i cannot be accessed because the scope is different, and after the display function is executed, local variable i will be recycled. The function of closure: "access local variables" and "prevent the memory occupied by the variables from being released"
//例 function fn(){ function fn(){ alert('hello'); } return fn; //返回fn函数首地址 } var test=fn(); //test也指向了fn函数的首地址 test();
Through Example 1, we know that a variable can point to the first address of a function, and a function can also return the first address of another function.
//例 function fn(){ var i = ; function fn(){ alert(i); } return fn; //返回fn函数首地址 } var test=fn(); //test也指向了fn函数的首地址 test();
We know from Example 2: Use a reject function to include variable i, so that the memory of local variable i will not be reclaimed.
//例 function fn(){ var i = ; function fn(){ alert(i++); } return fn; //返回fn函数首地址 } var test=fn(); //test也指向了fn函数的首地址 test(); test(); test();
In Example 3, because the memory of i will never be reclaimed, the value of i will be +1 every time fn2 is called. The result of the operation is that 10 pops up, 11 pops up, and 12 pops up.
Principle of closure: In Example 3, there are three scopes: global scope, fn1 scope, fn2 scope. There is test=fn1() in the global scope. In fact, this sentence is equivalent to test=fn2. There are var i=10 and return fn2 in the fn1 scope, and alert(i++) in the fn2 scope. When test=fn1() in the global scope is executed, test points to the scope of fn2. At this time, i under the fn2 scope is hooked by the global scope. According to the rules of the scope chain, i is not defined under fn2. , so i looked up the upper scope for i under fn2, and found var i=10 under fn1 scope. Therefore, the global test hooks the i of fn2, and the i of fn2 hooks the i of fn1, so fn1 will not be recycled after running.
4. Private attributes
In object-oriented thinking, some sensitive members that do not want to be made public can be defined as private, and this function can be simulated in JavaScript.
Syntax:
function Person(p_name){ var name = p_name; this.age }
var: private
this: public
function Person(p_name,p_age){ this.name = p_name; var age = p_age; } var p = new Person('zhangsan',); alert(p.name); alert(p.age);
In the above example, we want to use var to represent private member properties, but after the Person constructor is executed, age will be recycled and cannot be used as a member property.
function Person(p_name,p_age){ this.name = p_name; var age = p_age; this.setAge=function(a){ age = a; } this.getAge=function(){ return(age); } } var p = new Person('zhangsan',); p.setAge(); alert(p.getAge());
The two methods this.setAge and this.getAge use the local variable age, so age will not be recycled.
If there is only a set method, it means that the attribute is a write-only attribute.
If there is only a get method, it means that the attribute is a read-only attribute.
5. Use of call and apply
The functions of call and apply: call the current function using the specified object. The functions of call and apply are exactly the same, but they have slightly different syntax.
Syntax:
call([thisObj[,arg1[,arg2[,argN]]]])
The first parameter: who this points to when the function is executed
Parameters after: specify
in order as neededapply([thisObj[,argArray]])
The first parameter: who this points to when the function is executed
The second parameter: array, indicating the parameter set
In js, functions have several calling forms:
Person(); //Person内的this指向window var p=new Person(); //Person内的this指向p per.Person(); //Person内的this指向per function Person(p_name,p_age){ this.name = p_name; this.age = p_age; } function speak(){ alert(this.name + this.age); } var p = new Person('zhangsan',); //speak(); 这样调用this指向window //p.speak(); p对象没有speak属性
Use call and apply to call
function Person(p_name,p_age){ this.name = p_name; this.age = p_age; } function speak(){ alert(this.name + this.age); } var p = new Person('zhangsan',); speak.call(p); speak.apply(p);
call and apply do two things when executing: 1) point this inside the function to the first parameter 2) call the function
Also: You can also solve the problem like this:
P1.say=speak;
P1.say();
This solution is fundamentally different from the above solution:
The above solution is to call the speak function directly, but the pointer of this inside the function changes.
The following solution will add attributes to the p1 object, and the "volume" of the p1 object will become larger.
Example:
<script> function fn(){ this.style.color='red'; } function fn(){ this.style.fontSize='px'; } window.onload=function(){ document.getElementById('btn').onclick=function(){ var div = document.getElementById('div'); fn.call(div); fn.apply(div); }; }; </script> <div id='div'>hello javascript</div> <input type='button' id='btn' value='确定'>
6.继承的三种实现方法
概念:在有些面向对象语言中,可以使用一个类(子类)继承另一个类(父类),子类可以拥有父类的属性和方法,这个功能可以在js中进行模拟。
三种方法:
第一种:扩展Object方法
Object.prototype.方法=function(父类对象){ for(var i in 父类对象){ this[i] = 父类对象[i]; } };
举例说明:
Object.prototype.ext=function(parObject){ //循环遍历父类对象所有属性 for(var i in parObject){ //为子类对象添加这个遍历到的属性 //它的值是父类对象这个属性的属性值 this[i] = parObject[i]; } } function Person(p_name,p_age){ this.name=p_name; this.age=p_age; this.speak=function(){ alert(this.name+this.age); } } function Student(p_no){ this.no=p_no; this.say=function(){ alert(this.no+this.name_this.age); } } var stu = new Student(); stu.ext(new Person('xiaoqiang',)); stu.speak(); stu.say();
第二种:使用call和apply方法
语法:
父类构造器.call(this,.......);
function Person(p_name,p_age){ this.name=p_name; this.age=p_age; this.speak=function(){ alert(this.name+this.age); } } function Student(p_no,p_name,p_age){ this.no=p_no; this.say=function(){ alert(this.name+this.age+this.no); } Person.call(this,p_name,p_age); } var stu = new Student(,'zhagsan',); stu.speak(); stu.say();
第三种:原型继承
语法:
子类.prototype = new 父类();
function Person(p_name,p_age){ this.name=p_name; this.age=p_age; this.speak=function(){ alert(this.name+this.age); } } function Student(p_no){ this.no=p_no; this.say=function(){ alert(this.name+this.age+this.no); } } Student.prototype = new Person('wangwu',); var stu = new Student(); stu.speak(); stu.say();
以上内容给大家介绍了JS面向对象(3)之Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法,希望对大家有所帮助!

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.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

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.

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.