在JavaScript中,没有继承关键字: extends。那么,它是通过哪些方法,在用构造函数生成对象时,把对象基于另外一个构造函数,进行属性的生成(继承/拷贝)的呢? 即:对一个函数使用 new 关键字生成对象时,其对象的属性,可以来自于其它函数。
本文提供两种写法:
第一种(非正式):
但需要理解这种用法。
Javascript代码
function Animal (name, age){
this.name = name;
this.age = age;
}
function Dog (name, age){
this.i = Animal;
this.i(name, age);
}
d = new Dog('hello',20);
console.log(d);
/*
Dog {name: "hello", age: 20}
age: 20
i: function Animal(name, age)
name: "hello"
__proto__:
constructor: function Dog(name, age)
*/
上面一种写法是引用外部函数 Animal 作为自己的一个内部成员函数。
这是一种匿名函数的写法。
相当于:
Javascript代码
function Dog (name, age){
this.i = function Animal (name, age){
this.name = name;
this.age = age;
}
this.i(name, age);
}
相当于:
Javascript代码
function Dog (name, age){
/*
When calling a function, instead of using the 'new' keyword to create
object,
// Calling a function
Animal();
//Using the 'new' keyword to create object.
new Animal();
The inner 'this' is the same one of the outer 'this'. Because
there is no 'this' object created inside of the function, so it has to
refer to the outer one.
*/
this.i = function (name, age){
this.name = name; // 2. so the inner "this" is the same
this.age = age; // one of the outer "this".
}
this.i(name, age); // 1. function call, no 'this' created.
}
思考:调用函数时,"this"是谁??
既然,函数调用不生成 "this" 对象。
那么直接在 Dog 内调用 Animal 不可以吗?
答案:否
Java代码
/*
Which 'this' the called function belongs to, the inner 'this' inside of
the called function refers to that 'this' object.
*/
function Dog (name, age){ // if call Animal directly,
Animal(name,age); // the Animal function here belongs to 'window',
} // so 'this' inside of Animal refers to 'window'
第二种(正式):
使用 apply() 函数 或 call() 函数
apply() : apply "this" object to the(that) function, then call it.
Javascript代码
function Animal (name, age){
this.name = name;
this.age = age;
}
function Dog (name, age){
Animal.apply(this, arguments); // apply "this" object to Animal function.
// or
// call Animal function with a given 'this'
// object, instead of referring to other 'this'.
}
d = new Dog('hello',20);
console.log(d);
/*
Dog {name: "hello", age: 20}
age: 20
name: "hello"
__proto__:
constructor: function Dog(name, age)
*/

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),