中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

免費(fèi)咨詢問題的網(wǎng)站好的搜索引擎推薦

免費(fèi)咨詢問題的網(wǎng)站,好的搜索引擎推薦,凡客誠品的網(wǎng)站特色,建設(shè)商務(wù)網(wǎng)站的步驟odoo前端js對象的擴(kuò)展方法 在 Odoo 中,你可以使用兩種方法來擴(kuò)展 JavaScript 對象:extends 和 patch。這兩種方法在功能上有一些區(qū)別。 extends 方法: 使用 extends 方法可以創(chuàng)建一個新的 JavaScript 對象,并繼承自現(xiàn)有的對象。這…

odoo前端js對象的擴(kuò)展方法

在 Odoo 中,你可以使用兩種方法來擴(kuò)展 JavaScript 對象:extendspatch。這兩種方法在功能上有一些區(qū)別。

  1. extends 方法:

    • 使用 extends 方法可以創(chuàng)建一個新的 JavaScript 對象,并繼承自現(xiàn)有的對象。這意味著你可以在新對象中添加、修改或覆蓋現(xiàn)有對象的屬性和方法。
    • extends 方法適用于對現(xiàn)有對象進(jìn)行全面的擴(kuò)展和修改,以滿足特定的需求。你可以通過創(chuàng)建一個新的對象,并在其中添加自定義的屬性和方法來實(shí)現(xiàn)擴(kuò)展。
    • 使用 extends 方法時,你需要確保新對象的原型鏈正確設(shè)置,以便正確繼承和覆蓋現(xiàn)有對象的屬性和方法。
  2. patch 方法:

    • 使用 patch 方法可以直接修改現(xiàn)有的 JavaScript 對象,而無需創(chuàng)建新的對象。你可以通過 patch 方法來修改對象的屬性和方法,添加新的屬性和方法,或者刪除現(xiàn)有的屬性和方法。
    • patch 方法適用于對現(xiàn)有對象進(jìn)行局部的修改和調(diào)整,以滿足特定的需求。你可以直接在現(xiàn)有對象上進(jìn)行修改,而無需創(chuàng)建新的對象。
    • 使用 patch 方法時,你需要確保正確地定位到要修改的對象,并進(jìn)行相應(yīng)的修改操作。

總的來說,extends 方法適用于全面的擴(kuò)展和修改,而 patch 方法適用于局部的修改和調(diào)整。選擇使用哪種方法取決于你的具體需求和場景。

1、patch的實(shí)現(xiàn)

patch 一個簡單的例子

/** @odoo-module **/import { _t } from "@web/core/l10n/translation";
import { patch } from "@web/core/utils/patch";
import { SearchBar } from "@web/search/search_bar/search_bar";patch(SearchBar.prototype, {getPreposition(searchItem) {let preposition = super.getPreposition(searchItem);if (this.fields[searchItem.fieldName].name == 'payment_date') {preposition = _t("until");}return preposition}
});

2、patch.js

先學(xué)習(xí)一下object對象,再來學(xué)習(xí)patch.js

patch對象的原型,調(diào)用了很多Object的靜態(tài)方法,原則上就是在原型對象上動態(tài)增加或者替換新的屬性。

        // Replace the old property by the new one.Object.defineProperty(objToPatch, key, newProperty);

附錄: patch.js 源文件

/** @odoo-module **//***  @typedef {{*      originalProperties: Map<string, PropertyDescriptor>;*      skeleton: object;*      extensions: Set<object>;*  }} PatchDescription*//** @type {WeakMap<object, PatchDescription>} */
const patchDescriptions = new WeakMap();/*** Create or get the patch description for the given `objToPatch`.* @param {object} objToPatch* @returns {PatchDescription}*/
function getPatchDescription(objToPatch) {if (!patchDescriptions.has(objToPatch)) {patchDescriptions.set(objToPatch, {originalProperties: new Map(),skeleton: Object.create(Object.getPrototypeOf(objToPatch)),extensions: new Set(),});}return patchDescriptions.get(objToPatch);
}/*** @param {object} objToPatch* @returns {boolean}*/
function isClassPrototype(objToPatch) {// class A {}// isClassPrototype(A) === false// isClassPrototype(A.prototype) === true// isClassPrototype(new A()) === false// isClassPrototype({}) === falsereturn Object.hasOwn(objToPatch, "constructor") && objToPatch.constructor?.prototype === objToPatch;
}/*** Traverse the prototype chain to find a potential property.* @param {object} objToPatch* @param {string} key* @returns {object}*/
function findAncestorPropertyDescriptor(objToPatch, key) {let descriptor = null;let prototype = objToPatch;do {descriptor = Object.getOwnPropertyDescriptor(prototype, key);prototype = Object.getPrototypeOf(prototype);} while (!descriptor && prototype);return descriptor;
}/*** Patch an object** If the intent is to patch a class, don't forget to patch the prototype, unless* you want to patch static properties/methods.** @template T* @template {T} U* @param {T} objToPatch The object to patch* @param {U} extension The object containing the patched properties* @returns {() => void} Returns an unpatch function*/
export function patch(objToPatch, extension) {if (typeof extension === "string") {throw new Error(`Patch "${extension}": Second argument is not the patch name anymore, it should be the object containing the patched properties`);}const description = getPatchDescription(objToPatch);description.extensions.add(extension);const properties = Object.getOwnPropertyDescriptors(extension);for (const [key, newProperty] of Object.entries(properties)) {const oldProperty = Object.getOwnPropertyDescriptor(objToPatch, key);if (oldProperty) {// Store the old property on the skeleton.Object.defineProperty(description.skeleton, key, oldProperty);}if (!description.originalProperties.has(key)) {// Keep a trace of original property (prop before first patch), useful for unpatching.description.originalProperties.set(key, oldProperty);}if (isClassPrototype(objToPatch)) {// A property is enumerable on POJO ({ prop: 1 }) but not on classes (class A {}).// Here, we only check if we patch a class prototype.newProperty.enumerable = false;}if ((newProperty.get && 1) ^ (newProperty.set && 1)) {// get and set are defined together. If they are both defined// in the previous descriptor but only one in the new descriptor// then the other will be undefined so we need to apply the// previous descriptor in the new one.const ancestorProperty = findAncestorPropertyDescriptor(objToPatch, key);newProperty.get = newProperty.get ?? ancestorProperty?.get;newProperty.set = newProperty.set ?? ancestorProperty?.set;}// Replace the old property by the new one.Object.defineProperty(objToPatch, key, newProperty);}// Sets the current skeleton as the extension's prototype to make// `super` keyword working and then set extension as the new skeleton.description.skeleton = Object.setPrototypeOf(extension, description.skeleton);return () => {// Remove the description to start with a fresh base.patchDescriptions.delete(objToPatch);for (const [key, property] of description.originalProperties) {if (property) {// Restore the original property on the `objToPatch` object.Object.defineProperty(objToPatch, key, property);} else {// Or remove the property if it did not exist at first.delete objToPatch[key];}}// Re-apply the patches without the current one.description.extensions.delete(extension);for (const extension of description.extensions) {patch(objToPatch, extension);}};
}
http://www.risenshineclean.com/news/4852.html

相關(guān)文章:

  • 用wordpress開發(fā)網(wǎng)站錦繡大地seo官網(wǎng)
  • 服裝網(wǎng)站建設(shè)任務(wù)表網(wǎng)站權(quán)重一般有幾個等級
  • 外面網(wǎng)站怎么做怎么創(chuàng)建一個屬于自己的網(wǎng)站
  • 圣誕節(jié)網(wǎng)站怎么做百度反饋中心
  • 做網(wǎng)站的公司有怎么創(chuàng)建私人網(wǎng)站
  • 怎么做網(wǎng)站訊息it培訓(xùn)機(jī)構(gòu)培訓(xùn)費(fèi)用
  • 分析網(wǎng)站建設(shè)流程seo流量排行榜神器
  • 網(wǎng)絡(luò)營銷工具分析百度搜索關(guān)鍵詞排名優(yōu)化技術(shù)
  • 茂名專業(yè)網(wǎng)站制作公司seo的公司排名
  • 為什么凡科網(wǎng)做的網(wǎng)站無法搜索網(wǎng)站收錄入口申請查詢
  • 建立b2b企業(yè)網(wǎng)站百度網(wǎng)盤登陸入口
  • 建設(shè)高效的政府門戶網(wǎng)站深圳seo專家
  • 國內(nèi)移動端網(wǎng)站做的最好的抖音seo排名優(yōu)化
  • 帝國cms怎么做音樂網(wǎng)站如何查看一個網(wǎng)站的訪問量
  • 做金融網(wǎng)站有哪些要求網(wǎng)上的推廣公司
  • 外匯網(wǎng)站怎么做優(yōu)外匯網(wǎng)站怎么樣推廣自己的公司
  • 買東西哪個平臺質(zhì)量好seo助手
  • 無錫網(wǎng)站建設(shè)推廣重慶公司seo
  • 自助建站系統(tǒng)凡科百度權(quán)重高的發(fā)帖網(wǎng)站
  • 個人可以做外貿(mào)網(wǎng)站嗎福清網(wǎng)絡(luò)營銷
  • 注冊網(wǎng)站需要注意什么全渠道營銷
  • 哪些網(wǎng)站可以找到做海報的素材網(wǎng)絡(luò)營銷與直播電商是干什么的
  • 做網(wǎng)站需要幾個服務(wù)器百度推廣競價開戶
  • 產(chǎn)品做優(yōu)化好還是超級網(wǎng)站好龍崗網(wǎng)站制作
  • 陵園網(wǎng)站建設(shè)價格做網(wǎng)站的軟件叫什么
  • wordpress網(wǎng)站描述插件seo按照搜索引擎的
  • 如何建團(tuán)購網(wǎng)站網(wǎng)絡(luò)營銷是做什么的
  • 織夢如何做電商網(wǎng)站山東網(wǎng)絡(luò)優(yōu)化公司排名
  • 開發(fā)公司大廳售后長春seo網(wǎng)站管理
  • 抖音直播公會開放平臺蘇州網(wǎng)站關(guān)鍵字優(yōu)化