search
HomeWeb Front-endJS TutorialJS also plays with OO inheritance_javascript skills

After doing some calculation, I’m here anyway ^_^
Wuyou~~It’s been a long time….. Post something about the principle,


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

(stroll's follow-up)


Of course, you can also use the following methods without "plug-in"... However, you need to use Script5.5 to use
For example, call method and apply method... The following is how to use call The method to implement
apply is similar, except that the parameters called later are arrays. For details, you can refer to the MSScript5.6 instructions.

Bencalie, do you still remember when you told me how to use call apply? Is this answer satisfactory this time?

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

(stroll's follow-up)


Pay attention to the this.Prototype variable of the so-called "inherited object" (I didn't pay attention at first, thinking it was a keyword... which made me struggle to find information)

this. Prototype = new JSObject(); // Note: This is not a lowercase prototype
this.Prototype.Speak = function(s){.....}
Feedback directly after constructing the object return this .Prototype In fact, this is the JSPObject object.
is not a JSHuman object, so var o = new JSHuman();
o This instance cannot access the members of JSHuman
//westfly original //Don't ask me what's the use, just remember it when you need it one day functionJSObject() { this._Name = "JSObject"; this.Set_Name = function(Value){ this._Name = Value; } this.Get_Name = function(){ return this._Name; } } function JSHuman() // extends JSObject { this.Prototype = new JSObject(); // Note: this is not a lowercase prototype this.Prototype.Speak = function(s){ alert(s); } return this.Prototype; } var o = new JSHuman(); o.Set_Name("westfly"); o.Speak(o.Get_Name()); [Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

(Bencalie’s post)


stroll, I am very satisfied with your answer.

You raised the question again, so here Base.call(this, v_sName) is used as Base.apply(this), how do I pass v_sName to Base.BaseName, but I am a little bit confused about the use of apply. It’s hard to understand.

Finally, Base.apply(this, [v_sName]) was used to pass the array successfully

So it looks like call and apply are just different parameters?
(Roman’s post)
[Ctrl A Select all Note:
If you need to introduce external Js needs to be refreshed to execute
]
(bencalie’s follow-up)
[Ctrl A to select all Note: If you need to introduce external Js, you need to refresh to execute ]
(stroll的跟贴)


To bencalie ,是的,的确仅仅如此

不过不要认为apply这样是麻烦,其实如果两个对象的构造参数顺序一样的话,可以这么来
Base.apply(this, arguments) 很方便吧?

附上:(因为30秒才一贴~~)
不过你那里添加一个 new Base() 是认为无法继承的做法,其实是可以继承方法的,O.BaseMethod() 的调用就是了 ^_^
to:bencalie(Roman的跟贴)


基对象的方法被overwrite了.为什么stroll说 var o = new JSHuman(); o 这个实例是无法访问 JSHuman 的成员的
請看如下代碼:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

stroll,方法的確繼承過來了,但我在Son對象的搆造函數裏重寫了BaseMethod()

事實上O.BaseMethod()這樣調用已經不是Base對象的BaseMethod()方法了,而是Son對象的BaseMethod()方法,所以我把Base的一個實例儅作Son的成員base,使用

Son實例.base.BaseMethod()

的格式來引用Base的BaseMethod()方法


另外,我喜懽這句:Base.apply(this, arguments)
呵呵……收到~~(stroll的跟贴)


虽然说JS是没有指针了…… 不知道同时构造一个“父对象”,一个“子对象” 再有一个函数传递一个对象参数,这个参数使用父子共有的方法,是不是能够分别调用相应的方法呢? 还未了解JS 的 this 是不是相当于 虚拟地址的指针呢?
所以替换了方法才能解决上面的疑惑(虽然这个迷惑是菜了点,不过符合逻辑:)

还有……如果里面成员有构造对象的函式,是不是也会像c++一样先构造对象,然后再执行其他函式呢? 这个可能或则肯定是按照从上到下了~~~~语言规定?没试过

有没有试过多个父对象的继承呢?不过也不难想到这个一步……(树状继承)

这些就是偶的疑问啦,呵呵……
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

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 vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

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.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

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 in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

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.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

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 the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

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 vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

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 vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools