目錄
一、物件
#  1.字面量建立物件
  2.資料描述與存取描述設定
  3.get和set
  4.資料描述與存取描述檢查
二、prototype
  1.prototype釋義
  2.prototype與__proto__
之共享性" >  3.prototype之共享性
#之繼承性" >  4.prototype#之繼承性
三、類別
  1.類別的封裝
  1.混合的建構子/原型方法
  2.動態原型方法
  3.混合工廠方法
  4.再探討類別結構
  2.自訂類別
  3.類別的繼承
  1.拷貝傳承量物件(實例)
  2.call與apply實作物件繼承
  3.原型鏈繼承
  4.create實作類別繼承
  5.Object.create實作類別繼承  -- 建議的方式
  5.組合繼承-- 建議的方式
  6.繼承總結
#  7. prototype、constructor和__proto__在繼承中的關係
首頁 web前端 js教程 js中自訂物件的解析

js中自訂物件的解析

Jul 14, 2018 pm 03:01 PM

這篇文章主要介紹了關於js中自訂物件的解析,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

一、物件

#  1.字面量建立物件

var person = {
    name: "sun",
    age: 18,
    work: function () {
        console.log(this.name + "is working...");
    },
    address: {
        home: "大屯里xxx路xxx小区xx单元xxx室",
        phone: "123456789",
    }
};
person.work();
console.log(person.address.home);
登入後複製

  2.資料描述與存取描述設定

var person = {
    age: 18,
    address: {
        home: "大屯里xxx路xxx小区xx单元xxx室",
        phone: "123456789",
    }
};
Object.defineProperties(person, {
    name: {
        value: "sun",        // 该属性的值,可被读取
        writable: true,      // 表示能否修改属性的值,默认值为true
        configurable: true,  // 表示能否delete该属性并重新定义,直接在对象上定义的属性默认值为true
        enumerable: true     // 表示能否通过for-in枚举,直接在对象上定义的属性默认值为true
    },
    work: {
        value: function(){
            console.log(this.name + "is working...");
            },
        // 通过Object.defineProperty和Object.defineProperties定义属性,
        // 如果没有声明writable、configurable、enumerable,它们的默认值都是false
    }
});
person.work();
console.log(person.address.home);
登入後複製

  3.get和set

var circle = {
    value: 10,
    get girth(){
        return 2 * 3.14 * this.R
    },
    get area(){
        return 3.4 * this.R * this.R
    },
};
Object.defineProperty(circle, "R", {
    get : function () {
        return this.value;
    },
    set : function (val) {
        console.log("半径被修改了!");
        this.value = val;
    }
});
circle.R = 100;
console.log("girth: " + circle.girth + "area: " + circle.area);
登入後複製

  4.資料描述與存取描述檢查

var circle = {
    R: 10,
    // __proto__: null,
    get area(){
        return 3.4 * this.R * this.R
    },
};
Object.defineProperty(circle, "site", {
    value: [0, 2.2, 4.1],
    // enumerable: true,  // 是否可配置(读取),不设置为true时,Object.keys(circle))和Object.values(circle))将获取不到该键值对
});
console.log("R" in circle);  // 检查属性
console.log(circle.hasOwnProperty("R"));  // 检查自有的属性
console.log(circle.propertyIsEnumerable("R"));   // 检查属性是否是可枚举的
// Object对象的方法
console.log(Object.keys(circle));
console.log(Object.values(circle));
console.log(Object.getOwnPropertyNames(circle));  // 检查对象自身所有属性
console.log(Object.getOwnPropertyDescriptor(circle, "R")); // 得到circle对象关于R属性的描述
登入後複製

二、prototype

  1.prototype釋義

- 每一次创建函数,解析器都会向函数中添加一个属性:prototype
- 如果函数作为普通函数调用prototype,没有任何作用
- 当该函数以构造函数的形式调用时,它会有一个隐含的属性__proto__指向其原型对象
- 每个实例有各自的__proto__指向原型对象的prototype, 也就是原型对象中的属性和方法被调用函数"共享"
- 当类的原型对象prototype指向的内存地址发生改变时,已创建实例的__proto__ !== prototype,也就是不会被覆盖。而新创建的实例仍然是__proto__ === prototyp
登入後複製
function Person(name, age) {
    this.name = name;
    this.age = age;
}
// Person.prototype.gender = "male";
// Person.prototype.sayHello = function () {
//     return this.name + ", " + this.age + "years old."
// };
Person.prototype = {
    gender: "male",
    sayHello: function () {
        return this.name + ", " + this.age + "years old."
    }
};
var p1 = new Person("孙悟空", 2000);
p1.sayHello();
console.log(Person.prototype);
console.log(Person.prototype.constructor === Person);
登入後複製

  2.prototype與__proto__

  


function Person() {}
var obj1 = { gender: "male"};  // 创建两个内存地址
var obj2 = { age: 200 };
Person.prototype = obj1;
var p1 = new Person();
console.log(p1.__proto__ === Person.prototype);
console.log(p1.__proto__.gender);
console.log(Person.prototype);
Person.prototype = obj2;
var p2 = new Person();
console.log(p2.__proto__.age);
console.log(Person.prototype);
console.log(p1.__proto__.age);    // undefined
console.log(p2.__proto__.gender); // undefined
console.log(p1.__proto__ === Person.prototype);  // false,表示当prototype指向的内存地址改变时,已经创建的实例对象的__proto__仍指向原来的内存地址
console.log(p2.__proto__ === Person.prototype);
登入後複製
function Person() {}
Person.prototype = {name: "xxx", age: 100,};
var p1 = new Person();
console.log(p1.__proto__.name);
Person.prototype = { price: 998,};
var p2 = new Person();
console.log(p2.__proto__.price);
console.log(p1.__proto__.price);  // undefined
console.log(p2.__proto__.name);   // undefiend
console.log(p1.__proto__ === Person.prototype);  // false, 原型对象的内存地址引用已发生改变
console.log(p1.__proto__.age);    // __proto__指向的内存地址被保留
console.log(p2.__proto__ === Person.prototype);  // true
登入後複製
function Person() {}
Person.prototype = { price: 60 };
var p1 = new Person();
Person.prototype = { price: 998};
var p2 = new Person();
console.log(p1.__proto__ === Person.prototype);  // 依然是false
console.log(p2.__proto__ === Person.prototype);  // true
登入後複製

  3.prototype之共享性

// prototype非常类似python中的静态属性和静态方法。每个实例都可以访问同一块内存空间。
function Person() {}
Person.prototype = {price: 60};
var p1 = new Person();
var p2 = new Person();
console.log(p1.__proto__.price);
console.log(p2.__proto__.price);
console.log(Person.prototype.price);
登入後複製

  4.prototype#之繼承性

// 当访问实例对象的一个属性或方法时,它会先在对象自身中查找,如果有则直接使用;如果没有则在原型对象中继续查找,如果有则直接使用
function Person() {}
Person.prototype = {price: 60};
var p1 = new Person();
var p2 = new Person();
console.log(p1.price);
console.log(p2.price);
console.log(Person.prototype.price);
登入後複製

三、類別

  1.類別的封裝

// 字面量方法(工厂方法) -- 直接在var obj = {}内部写代码,缺点是只实例化一次
// 构造函数方法        -- 只用构造函数声明this,缺点是可扩展性差,数据重复
// 原型方法           -- 只用prototype声明共有的属性和方法,缺点是实例的数据相同,不满足多态
登入後複製

  1.混合的建構子/原型方法

// 最广泛的使用方法
function Person(name, age) {
    this.name = name;
    this.age = age;
}
// prototype写在外面是为了保证其动态增加公共属性和方法
Person.prototype.sayHello = function () {
    console.log(this.name + ", " + this.age + " years old.");  // 把共有的属性和方法封装到prototype中
};
var p = new Person("孙悟空", 2000);
p.sayHello();
登入後複製
// 我把它写给Person的属性,让父类也能够访问
function Person(name, age) {
    Person.group = Person.prototype.group = "西天取经组";
    Person.toString = Person.prototype.toString = function (){
        console.log("Person: " + Person.group)
    };
    this.name = name;
    this.age = age;
    this.sayHello = function () {
      console.log(this.name + ", " + this.age + "years old.")
    };
}
var person = new Person("孙悟空", 2000);
console.log(person.constructor); // 检查构造器函数
console.log(person instanceof Person);  // 检查是否为其原型类
person.sayHello();
Person.toString();
登入後複製

  2.動態原型方法

// 也是常用的方法
function Person(name, age) {
    this.name = name;
    this.age = age;
    if (typeof Person._initialized === "undefined"){
        Person.prototype.sayHello = function () {
            console.log(this.name + ", " + this.age + " years old.");
        };
        Person._initialized = true;
    }
}
var p = new Person("孙悟空", 2000);
p.sayHello();
登入後複製

  3.混合工廠方法

// 混合工厂方法  -- 存在与工厂方法类似的问题,不建议使用
function Person(name, age) {
    var obj = {};
    obj.name = name;
    obj.age = age;
    obj.sayHello = function () {
        console.log(this.name + ", " + this.age + " years old.");
    };
    return obj
}
var p = new Person("孙悟空", 2000);
p.sayHello();
登入後複製

  4.再探討類別結構

function Person(name, age) {
    // 静态属性
    Person.group = "西天取经四人组,暗合金木水火土";
    // 静态方法
    Person.introduce = function () {
        console.log("贫僧自东土大唐而来")
    };
    // 实例属性
    this.name = name;
    this.age = age;
    // 实例方法,应该写在prototype中
    this.say = function () {
        console.log("hello, i'm " + this.name);
    };
    Person.prototype.introduce = Person.introduce; // 此时Person类和其实例都可以使用introduce方法
    // 父类使用实例方法
    Person.example = Person.prototype.example = function (self) {
        self = self || this;
        console.log(self.name + " " + self.age);
    }
}
// 在python中,实例可以访问父类的属性和方法,父类也可以使用实例方法
// 在java和js中,实例不能调用父类的静态属性和静态方法,父类不能使用实例方法
// 如果想让实例和父类共享一个属性或者方法,就只能放到方法区并创建引用
var sun = new Person("孙悟空", 2000);
Person.introduce();  // 父类调用静态方法
sun.say();
sun.introduce();     // 实例调用静态方法
Person.example(sun); // 父类调用实例方法
sun.example();       // 子类调用实例方法
// 可见,prototype是父类和实例的沟通桥梁
登入後複製

  2.自訂類別

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.sayHello = function () {
      console.log(this.name + ", " + this.age + "years old.")
    };
}
function New(Person) {
    return function () {
        var obj = {"__proto__": Person.prototype};  // 必须写在这里
        Person.apply(obj, arguments);  // arguments同this一样,是默认自带的关键字,用于存储传入的参数
        return obj
    }
}
var temp = New(Person);
var p1 = temp("孙悟空", 2000);
var p2 = temp("猪八戒", 1);
p1.sayHello();
p2.sayHello();
登入後複製

  3.類別的繼承

  1.拷貝傳承量物件(實例)

var person = {
    name: "Li",
    age: 16,
    address: {
        home: "none",
        city: "none",
    },
    say: function(){
        console.log("hello, guy.")
    }
};
var child = {gender:"female",};
function extendDeeply (p, c){
    var c = c || {};
    for (var prop in p) {
        if (typeof p[prop] === "object") {
            c[prop] = (p[prop].constructor === Array) ? [] : {};
            extendDeeply(p[prop], c[prop]);
        } else {
            c[prop] = p[prop];
        }
    }
}
extendDeeply(person, child);
console.log(child);
child.say();
登入後複製

  2.call與apply實作物件繼承

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.address = {
        home: "none",
        city: "none",
    }
}
Person.prototype.say = function () {
    console.log("hello, guy.")
};
// 它继承的只是实例对象this,无法继承父类原型prototyp
function Child(name, age) {
    Person.call(this, name, age);
    this.gender = "female";
}
var child = new Child("Li", 16);
console.log(child);
// child.say(); 报错: child.say is not a function.
登入後複製
对象继承的缺点:只继承了实例对象的可访问的属性和方法,没有继承原型
登入後複製

  3.原型鏈繼承

// 原型链继承
function Person() {}
Person.prototype.name = "Person";
Person.prototype.toString = function () {
    console.log(this.name);
};
function Child(name, age) {
    this.age = age;
    this.name = name;
}
Child.prototype = Person.prototype;
Child.prototype.constructor = Child;
var child = new Child("Li", 16);
console.log(child.name + " " + child.age);
child.toString();
// 其缺点是之继承了原型,没有继承实例
登入後複製

  4.create實作類別繼承

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.address = {
        home: "none",
        city: "none",
    }
}
Person.prototype.say = function () {
    console.log("hello, guy.")
};
function Child(P, name, age) {
    function F() {}
    F.prototype = new P(name, age);
    var c = new F();
    return c;
}
Child.prototype.constructor = Child;  // 无法修正
var child = new Child(Person, "Li", 16);
console.log(child);
console.log(child.name);
child.say();
console.log(child.constructor);  // 结果为[Function: Person],构造器指向无法修正
console.log(child instanceof Child);  // false
console.log(child instanceof Person); // true
登入後複製

  5.Object.create實作類別繼承  -- 建議的方式

// Object.create继承,实现原理和上面的create类似
// 1.创建父类
function Person() {}
Person.prototype.sayPerson = function () {
    console.log("hello, Person.")
};
// 2.创建子类
function Child(gender) {this.gender = gender;}
// 3.create继承
// Object.create的第二个参数是属性描述
Child.prototype = Object.create(Person.prototype, {
    name: {
        value: "Li",
        writable: true,
        enumerable: true,
        configurable: true,
    },
    age: {
        value: 16,
        writable:true,
        configurable:true,
        enumerable:true,
    },
});  // 重写子类prototype
Child.prototype.constructor = Child;  // constructor 修正
// 4.在create之后写子类的prototype
Child.prototype.sayChild = function () {
    console.log("hello, Child.")
};
var child = new Child("female");
console.log(child);
console.log(child.name + " " + child.age);
child.sayChild();
child.sayPerson();
登入後複製

  5.組合繼承-- 建議的方式

function Person(name, age) {
    this.name =name;
    this.age = age;
}
Person.prototype.toString = function () {
    console.log(this.name + " " + this.age);
};
function Child(name, age, gender) {
    Person.call(this, name, age);
    this.gender = gender;
}
Child.prototype = new Person();  // new时不传参数,是为了只继承原型,即Child.prototype = Person.prototype
// Child.prototype = Person.prototype;  // 两者等价
Child.prototype.constructor = Child;
var child = new Child("Li", 16, "female");
console.log(child);
child.toString();
console.log(child instanceof Child);   // true
console.log(child instanceof Person);  // true
登入後複製

  6.繼承總結

rr]

#  7. prototype、constructor和__proto__在繼承中的關係

#以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關建議:

 對js中事件模型的解析

如何將js變數值傳到php

以上是js中自訂物件的解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何創建和發布自己的JavaScript庫? 如何創建和發布自己的JavaScript庫? Mar 18, 2025 pm 03:12 PM

文章討論了創建,發布和維護JavaScript庫,專注於計劃,開發,測試,文檔和促銷策略。

如何在瀏覽器中優化JavaScript代碼以進行性能? 如何在瀏覽器中優化JavaScript代碼以進行性能? Mar 18, 2025 pm 03:14 PM

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

前端熱敏紙小票打印遇到亂碼問題怎麼辦? 前端熱敏紙小票打印遇到亂碼問題怎麼辦? Apr 04, 2025 pm 02:42 PM

前端熱敏紙小票打印的常見問題與解決方案在前端開發中,小票打印是一個常見的需求。然而,很多開發者在實...

如何使用瀏覽器開發人員工具有效調試JavaScript代碼? 如何使用瀏覽器開發人員工具有效調試JavaScript代碼? Mar 18, 2025 pm 03:16 PM

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

如何有效地使用Java的收藏框架? 如何有效地使用Java的收藏框架? Mar 13, 2025 pm 12:28 PM

本文探討了Java收藏框架的有效使用。 它強調根據數據結構,性能需求和線程安全選擇適當的收集(列表,設置,地圖,隊列)。 通過高效優化收集用法

如何使用源地圖調試縮小JavaScript代碼? 如何使用源地圖調試縮小JavaScript代碼? Mar 18, 2025 pm 03:17 PM

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

開始使用Chart.js:PIE,DONUT和BUBBLE圖表 開始使用Chart.js:PIE,DONUT和BUBBLE圖表 Mar 15, 2025 am 09:19 AM

本教程將介紹如何使用 Chart.js 創建餅圖、環形圖和氣泡圖。此前,我們已學習了 Chart.js 的四種圖表類型:折線圖和條形圖(教程二),以及雷達圖和極地區域圖(教程三)。 創建餅圖和環形圖 餅圖和環形圖非常適合展示某個整體被劃分為不同部分的比例。例如,可以使用餅圖展示野生動物園中雄獅、雌獅和幼獅的百分比,或不同候選人在選舉中獲得的投票百分比。 餅圖僅適用於比較單個參數或數據集。需要注意的是,餅圖無法繪製值為零的實體,因為餅圖中扇形的角度取決於數據點的數值大小。這意味著任何占比為零的實體

誰得到更多的Python或JavaScript? 誰得到更多的Python或JavaScript? Apr 04, 2025 am 12:09 AM

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

See all articles