
地址:前端面试题库
前言
散列表(Hash table,也叫哈希表),是根据键(Key)而直接访问在内存储存位置的数据结构。也就是说,它通过计算出一个键值的函数,将所需查询的数据映射到表中一个位置来让人访问,这加快了查找速度。这个映射函数称做散列函数,存放记录的数组称做散列表。
- // 创建一个Object的新实例,然后在实例上面添加属性和方法
- const person = new Object();
- person.name = "moment";
- person.age = 18;
- person.running = function () {
- console.log("我会游泳哦,你会吗");
- };
- 复制代码
- const person = {
- running: function () {
- console.log("你会我也会啊");
- },
- name: "moment",
- age: 7,
- dance: function () {
- console.log("我还会跳舞呢");
- },
- };
- 复制代码
- const running = (info) => {
- console.log(info);
- };
- const person = new Object();
- person.name = "moment";
- person.age = 18;
- person.running = running;
-
- const object = {
- running,
- name: "moment",
- age: 7,
- dance: function () {
- console.log("我还会跳舞呢");
- },
- };
-
- // ,似乎只能封装公共的方法,属性无法动态传值,只能是固定的一个值
-
- person.running("我们大家都会游泳哦"); // 我们大家都会游泳哦
- object.running("我们大家都会游泳哦"); // 我们大家都会游泳哦
- 复制代码
抽象工厂模式(英语:Abstract factory pattern)是一种软件开发设计模式。抽象工厂模式提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来。在正常使用中,客户端程序需要创建抽象工厂的具体实现,然后使用抽象工厂作为接口来创建这一主题的具体对象。客户端程序不需要知道(或关心)它从这些内部的工厂方法中获得对象的具体类型,因为客户端程序仅使用这些对象的通用接口。抽象工厂模式将一组对象的实现细节与他们的一般使用分离开来。
- function createObject(name, age, info) {
- const obj = new Object();
- obj.name = name;
- obj.age = age;
-
- obj.running = function () {
- console.log(info);
- };
-
- return obj;
- }
-
- const person = createObject("moment", 18, "我会跑步");
- const student = createObject("supper", 16, "我会跑步,我比你还年轻呢");
-
- person.running(); // 我会跑步
- student.running(); // 我会跑步,我比你还年轻呢
- 复制代码
- function Person(name, age, info) {
- this.name = name;
- this.age = age;
-
- this.running = function () {
- console.log(info);
- };
- }
-
- const person = new Person("moment", 18, "我会跑步");
- const student = new Person("supper", 16, "我会跑步,我比你还年轻呢");
-
- person.running(); // 我会跑步
- student.running(); // 我会跑步,我比你还年轻呢
- 复制代码
- person.name = "moment";
- person.age = 18;
- person.running = function () {
- console.log("我会跑步");
- };
-
- student.name = "moment";
- student.age = 16;
- student.running = function () {
- console.log("我会跑步,我比你还年轻呢");
- };
- 复制代码
- function myNew(func, ...args) {
- // 判断方法体
- if (typeof func !== "function") {
- throw "第一个参数必须是方法体";
- }
- // 创建新对象
- // 这个对象的[[prototype]](隐式原型 __proto__)指向 func 这个类的原型对象 prototype
- // 即实例可以访问构造函数原型 obj.constructor === Person
- const object = Object.create(func.prototype);
-
- // 构造函数内部的this被赋值为这个新对象
- const result = func.apply(object, args);
-
- // 如果构造函数返回的结果是引用数据类型,则返回运行后的结果
- // 否则返回新创建的 obj
- const isObject = typeof result === "object" && result !== null;
- const isFunction = typeof result === "function";
-
- return isObject || isFunction ? result : object;
- }
- 复制代码
- // instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上
- // person和student的隐式原型都指向Person的显式原型;
- console.log(person.__proto__ === student.__proto__); //true
- console.log(person instanceof Object); //true
- console.log(person instanceof Person); //true
- console.log(student instanceof Object); //true
- console.log(student instanceof Person); //true
-
- // 以上代码的代码实际上执行的该例子
- console.log(student.__proto__.__proto__.constructor.prototype === Object.prototype);
- console.log(student.__proto__.constructor.prototype === Person.prototype);
- 复制代码
- console.log(student.running === person.running); //false
- 复制代码
- function Person(name, age) {
- this.name = name;
- this.age = age;
- }
-
- // 在这里,把方法running定义在了Person的prototype属性上
- // person和student共享同一个原型方法running,指向的是同一快内存空间
- Person.prototype.running = function (info) {
- console.log(info);
- };
-
- const person = new Person("moment", 18);
- const student = new Person("supper", 16);
-
- // 在构造函数中这里输出的是false
- console.log(person.running === student.running); //true
-
- person.running("我会跑步"); // 我会跑步
- student.running("我会跑步,我比你还年轻呢"); //我会跑步,我比你还年轻呢
- 复制代码


- console.log(Person.prototype.constructor === Person); // true
- console.log(student.__proto__.constructor === Person); // true
- 复制代码


- function Person(name, age) {
- this.name = name;
- this.age = age;
- }
- // 在这里,把方法running定义在了Person的prototype属性上了
- Person.prototype.running = function (info) {
- console.log(info);
- };
-
- const obj = {};
-
- const person = new Person("moment", 18);
- const student = new Person("supper", 16);
-
- console.log(obj.__proto__ === Object.prototype); // true
- console.log(Object.constructor === Function); // true
- console.log(Function.prototype.__proto__ === Object.prototype); // true
- console.log(Function.constructor === Function); // true
- console.log(Person.__proto__.constructor.__proto__ === Function.prototype); // true
- console.log(student.__proto__.__proto__.constructor.__proto__ === Function.prototype); // true
- console.log(Function.constructor.__proto__ === Function.prototype); // true
- console.log(Object.constructor === Function.constructor); // true
- console.log(Object instanceof Function); // true
- console.log(Function instanceof Object); // true
- console.log(Function.prototype.__proto__ === Object.prototype); // true
- 复制代码
- function Person(name, age) {
- this.name = name;
- this.age = age;
- this.sayName = function () {
- console.log("我是在实例身上的");
- };
- this.memory = "我是属于实例的";
- }
- // 在这里,把方法running定义在了Person的prototype属性上了
- Person.prototype.running = function () {
- console.log("我是原型上的方法");
- };
-
- Object.prototype.牛逼 = "这是真的";
-
- Person.prototype.memory = "我是属于原型的";
-
- const person = new Person("moment", 18);
-
- console.log(person.name); // 来自实例
- console.log(person.memory); // 来自实例
- person.sayName(); // 来自实例
- person.running(); // 来自原型
- console.log(person.牛逼); // 这是真的
- console.log(person.六六六); // undefined
- 复制代码
- function Student() {}
- function Teacher() {}
-
- Student.prototype.running = function () {
- console.log("学生");
- };
- Student.prototype.eating = function () {
- console.log("吃");
- };
- Student.prototype.study = function () {
- console.log("学习");
- };
-
- Teacher.prototype.running = function () {
- console.log("老师
- };
- Teacher.prototype.teach = function () {
- console.log("吃");
- };
- 复制代码
- function Student() {}
- function Teacher() {}
-
- Teacher.prototype.running = function () {
- console.log("老师");
- };
- Teacher.prototype.teach = function () {
- console.log("吃");
- };
-
- Student.prototype = Teacher.prototype;
-
- const student = new Student();
- student.running(); // 老师
-
- student.__proto__.running = function () {
- console.log("我被修改了");
- };
-
- const teacher = new Teacher();
- teacher.running();// 我被修改了
- 复制代码
- function Student() {}
- function Teacher() {}
-
- Teacher.prototype.running = function () {
- console.log("老师");
- };
-
- Teacher.prototype.teach = function () {
- console.log("教");
- };
-
- const teach = new Teacher();
- Student.prototype = teach;
-
- const student = new Student();
-
- student.running = function () {
- console.log("我被修改了");
- };
-
- const smallStudent = new Student();
- smallStudent.running(); // 老师 继承于Teacher原型
- student.running(); // 我被修改了 来自于实例本身
- 复制代码

- function Teacher(nickname, age, height) {
- this.nickname = nickname;
- this.age = age;
- this.height = height;
- }
-
- function Student(nickname, age, height) {
- Teacher.call(this, nickname, age, height);
- this.hobby = ["唱", "跳", "rap"];
- }
-
- Teacher.prototype.running = function () {
- console.log("老师");
- };
-
- Teacher.prototype.teach = function () {
- console.log("教");
- };
-
- const student = new Student("moment", "18", "1米59");
-
- console.log(student.height); // 1米59
- console.log(student.hobby); // ["唱", "跳", "rap"]
- 复制代码
- function Teacher(nickname, age, height) {
- this.nickname = nickname;
- this.age = age;
- this.height = height;
- }
-
- function Student(nickname, age, height) {
- Teacher.call(this, nickname, age, height);
- this.hobby = ["唱", "跳", "rap"];
- }
-
- Teacher.prototype.running = function () {
- console.log("老师");
- };
-
- Teacher.prototype.teach = function () {
- console.log("教");
- };
-
- Student.prototype = new Teacher();
-
- const student = new Student("moment", "18", "1米59");
- console.log(student.height); // 1米59
- console.log(student.hobby); // ["唱", "跳", "rap"]
- 复制代码
- function inheritPrototype(superType, children) {
- const prototype = Object(superType.prototype); // 创建对象
- prototype.constructor = children; // 增强对象
- children.prototype = prototype; // 赋值对象
- }
-
- function Teacher(nickname, age, height) {
- this.nickname = nickname;
- }
-
- function Student(nickname) {
- Teacher.call(this, nickname);
- this.hobby = ["唱", "跳", "rap"];
- }
-
- inheritPrototype(Student, Teacher);
-
- Teacher.prototype.running = function () {
- console.log("老师会跑步");
- };
-
- Student.prototype.running = function () {
- console.log("学生也会跑步");
- };
-
- const student = new Student("moment");
-
- student.running(); // 学生也会跑步
- console.log(student.hobby); // ['唱', '跳', 'rap']
- console.log(student.nickname); // comment
- 复制代码
地址:前端面试题库