假設我在名為main.js的文件中有類似的內容: 函數obj_name(){} obj_name.prototype = { foo:function(){alert('hi!'); }, foo2:function(){alert('嗨,再見!'); } } 現在,我正在嘗試通過這種方式將對象擴展到另一個文件extend.js中: obj_name.prototype = { newfoo:function(){alert('hi#3'); } } ...但是問題是,如果我以這種方式編寫代碼,它將可以正常工作: obj_name.prototype.newfoo = function(){alert('hi#3'); } 我想這可能是一個菜鳥問題。我什至不知道這是否是擴展對象的正確方法,但是我很想知道為什麼會這樣。 預先謝謝你們。
2021-01-06 08:10:13
第一種方法是,用新的原型替換原型(覆蓋以前的原型)。在第二種方法中,您要向原型添加一個新成員(從而進行擴展)。 還有另一種方法:具有擴展方法或類似方法的庫(基本上是將第二種形式的工作用一個好的包裝器包裝)。例如,在jQuery中: $ .extend(obj_name.prototype,{ newfoo:function(){alert('hi#3'); } } | 沒有jQuery的另一種選擇: var extend = function(目標,源) { 為(源代碼中的var屬性) { if(目的地[屬性] &&(typeof(目的地[屬性])=='對象') &&(destination [property] .toString()=='[object Object]')&& source [property]) 擴展(目的地[屬性],源[屬性]); 其他 destination [property] = source [property]; } 返回目的地; } var a = {a:'test'}; // 原版的 var b = {newFoo:function(){alert('hi#3'); }}; //加法 擴展(a,b); //擴展它 a.newFoo(); //調用添加的屬性 | 這是因為在行上 obj_name.prototype = { newfoo:function(){alert('hi#3'); } } 您創建一個新的原型對象,刪除私有內容。好像你已經說過 var a = {}; 當你擴展像 obj_name.prototype.newfoo = function(){alert('hi#3'); } 它只是向對象樹添加一個新屬性(newfoo),使現有內容保持不變。這就是為什麼它起作用 高溫超導 伊沃·斯托伊科夫(Ivo Stoykov) | 如果您正在尋找一個簡單的輕量級庫來提供給您確切的信息:javascript中的OOP“正確完成”,請看一下:https://github.com/haroldiedema/joii github頁面上的自述文件中提供的源代碼示例以及以下鏈接: http://harold.info/projects/joii http://haroldiedema.github.io/joii 該庫基本上允許您這樣定義“類”: var Person = Class(function(){ this.firstname =“約翰” this.surname =“史密斯” this.role =“開發人員” this.getInfo = function(){ 返回this.firstname +''+ this.surname +'is'+ this.role; }; }); var AnotherPerson = Class({擴展:Person},function(){ this.firstname =“鮑勃”; }); var p = new AnotherPerson(); console.log(p.getInfo()); // Bob Smith是開發人員 編輯 以您的代碼為例,但將其轉換為與JOII兼容的代碼,它看起來就像這樣: var obj_name = Class(function(){ this.foo = function(){alert('hi!'); }; this.foo2 = function(){alert('嗨,再見!'); }; }; var obj_name2 = Class({擴展名:obj_name},function(){ this.newfoo = function(){alert('hi#3'); }; }); var o = new obj_name2(); o.foo(); //嗨! o.newfoo(); //嗨#3 或將其用作混入: var o = new obj_name(); o.mixin(obj_name2); o.newfoo(); //嗨#3 或者相反,使用“特徵”。 //“使用”選項基本上將內容從給定對象複製到“類”的範圍,從而解決了水平代碼重用問題。 var obj_name = Class({使用:[obj_name2],function(){ this.foo = function(){alert('hi!'); }; this.foo2 = function(){alert('嗨,再見!'); }; }); var o = new obj_name(); o.newfoo(); //嗨#3 | 你的答案 StackExchange.ifUsing(“ editor”,function(){ StackExchange.using(“ externalEditor”,function(){ StackExchange.using(“ snippets”,function(){ StackExchange.snippets.init(); }); }); },“代碼段”); StackExchange.ready(function(){ var channelOptions = { 標籤:“” .split(“”), id:“ 1” }; initTagRenderer(“”。split(“”),“” .split(“”),channelOptions); StackExchange.using(“ externalEditor”,function(){ //如果啟用了摘要,則必須在摘要後觸發編輯器 如果(StackExchange.settings.snippets.snippetsEnabled){ StackExchange.using(“ snippets”,function(){ createEditor(); }); } 其他{ createEditor(); } }); 函數createEditor(){ StackExchange.prepareEditor({ useStacksEditor:否, heartbeatType:“答案”, autoActivateHeartbeat:否, convertImagesToLinks:是, noModals:是的, showLowRepImageUploadWarning:是的, 聲望:ToPostImages:10, bindNavPrevention:正確, 後綴:“”, imageUploader:{ brandingHtml:“採用\ u003ca href = \“ https://imgur.com/ \” \ u003e \ u003csvg class = \“ svg-icon \” width = \“ 50 \” height = \“ 18 \” viewBox = \“ 0 0 50 18 \” fill = \“ none \” xmlns = \“ http://www.w3.org/2000/svg \” \ u003e \ u003cpath d = \“ M46.1709 9.17788C46.1709 8.26454 46.2665 7.94324 47.1084 7.58816C47.4091 7.46349 47.7169 7.36433 48.0099 7.26993C48.9099 6.97997 49.672 6.73443 49.672 5.93063C49.672 5.22043 48.9832 4.61182 48.1414 4.61182C47.4335 4.61182 46.7256 4.91628 46.0943 5.50789C45.74.3 4.313.6662 4.313.6662 43.1481 6.59048V11.9512C43.1481 13.2535 43.6264 13.8962 44.6595 13.8962C45.6924 13.8962 46.1709 13.2535 46.1709 11.9512V9.17788Z \“ / \ u003e \ u003cpath d = \” M32.492 10.1419C32.492 12.6954 34.1182 14.048437.0451 14.0484 41.5985 12.6954 41.5985 10.1419V6.59049C41.59855.28821 41.1394 4.66232 40.1061 4.66232C39.0732 4.66232 38.5948 5.28821 38.5948 6.59049V9.60062C38.5948 10.8521 38.2696 11.5455 37.0451 11.5455C35.8209 11.5455 35.4954 10.8521 35.4954 9.60062V6.59049C35.4954 5.28821 35.0173 4.66232 3.49492 .1419Z \“ / \ u003e \ u003cpath fill-rule = \” evenodd \“ clip-rule = \” evenodd \“ d = \” M25.6622 17.6335C27.8049 17.6335 29.3739 16.9402 30.2537 15.6379C30.8468 14.7755 30.9615 13.5579 30.9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C28.9913 4.66231 28.4555 4.94978 28.1109 5.50789C27.499 4.86533 26.7335 4.56087 25.7005 4.56087C23.1369 4.56087 21.0134 6.57349 21.0134 9.27932C21.0134 11.9852 13.003 13.913 25.3607 12.6616C28.1109 12.7229 28.1161 12.7799 28.121 12.8346C28.1256 12.8854 28.1301 12.9342 28.1301 12.983C28.1301 14.4373 27.2502 15.2321 25.777 15.2321C24.8349 15.2321 24.1352 14.9821 23.9821 23.5661 14.7787C23.176 14.6393 22.8472 14.5218 22.54 37 14.5218C21.7977 14.5218 21.2429 15.0123 21.2429 15.6887C21.2429 16.7375 22.9072 17.6335 25.6622 17.6335ZM24.1317 9.27932C24.1317 7.94324 24.9928 7.09766 26.1024 7.09766C27.2119 7.09766 28.0918 7.94324 28.0918 9.27932C28.0918 1.511621161.716 24.1317 10.6491 24.1317 9.27932Z \“ / \ u003e \ u003cpath d = \” M16.8045 11.9512C16.8045 13.2535 17.2637 13.8962 18.2965 13.8962C19.3298 13.8962 19.8079 13.2535 19.8079 11.9512V8.12928C19.8079 5.82936 18.4879 4.62866 16.527 14.279 4.98375 13.3609 5.88013C12.653 5.05154 11.6581 4.62866 10.3573 4.62866C9.34336 4.62866 8.57809 4.89931 7.9466 5.5079C7.58314 4.9328 7.10506 4.66232 6.51203 4.66232C5.47873 4.66232 5.00066 5.28821 5.00066 6.59049V11.9512C5.00066 3.962 3.962 8.0232 13.2535 8.0232 11.9512V8.90741C8.0232 7.58817 8.44431 6.91179 9.53458 6.91179C10.5104 6.91179 10.893 7.58817 10.893 8.94108V11.9512C10.893 13.2535 11.3711 13.8962 12.4044 13.896 2C13.4375 13.8962 13.9157 13.2535 13.9157 11.9512V8.90741C13.9157 7.58817 14.3365 6.91179 15.4269 6.91179C16.4027 6.91179 16.8045 7.58817 16.8045 8.94108V11.9512Z \“ / \ u003e \ u003cpath d = \” M3.31675 6.59049C3.31675662 1.82471 4.66232C0.791758 4.66232 0.313354 5.28821 0.313354 6.59049V11.9512C0.313354 13.2535 0.791758 13.8962 1.82471 13.8962C2.85798 13.8962 3.31675 13.2535 3.31675 11.9512V6.59049Z \“ / \ u003e \ u003cpath d = \” M1.843612 0.400291C 0 1.1159 0 1.98861C0 2.87869 0.822846 3.57676 1.87209 3.57676C2.90056 3.57676 3.7234 2.87869 3.7234 1.98861C3.7234 1.1159 2.90056 0.400291 1.87209 0.400291Z \“ fill = \”#1BB76E \“ // u003e \ u003c / svg \ u003e \ u003c / a \ u003e”, contentPolicyHtml:“根據\ u003ca href = \“ https://stackoverflow.com/help/licensing \” \ u003ecc by-sa \ u003c / a \ u003e \ u003ca href = \“ https://stackoverflow.com獲得許可的用戶貢獻/ legal / content-policy \“ \ u003e(內容策略)\ u003c / a \ u003e”, allowUrls:是 }, onDemand:是的, dispatchSelector:“。discard-answer” ,立即ShowMarkdownHelp:true,enableTables:true,enableSnippets:true }); } }); 感謝您為Stack Overflow提供答案! 請務必回答問題。提供詳細信息並分享您的研究! 但是要避免... 尋求幫助,澄清或回答其他答案。 根據意見發表聲明;用參考或個人經驗來備份它們。 要了解更多信息,請參見我們撰寫出色答案的提示。 草稿已保存 草稿丟棄 註冊或登錄 StackExchange.ready(function(){ StackExchange.helpers.onClickDraftSave('#login-link'); }); 使用Google註冊 使用Facebook註冊 使用電子郵件和密碼註冊 提交 以訪客身份發布 名稱 電子郵件 必需,但從未顯示 StackExchange.ready( 函數(){ StackExchange.openid.initPostLogin('。new-post-login','https%3a%2f%2fstackoverflow.com%2fquestions%2f5975408%2fhow-can-i-extend-properly-a-js-object%23new-answer' ,“ question_page”); } ); 以訪客身份發布 名稱 電子郵件 必需,但從未顯示 發表您的答案 丟棄 點擊“發布答案”,即表示您同意我們的服務條款,隱私政策和Cookie政策 不是您要找的答案?瀏覽標記為javascript javascript-objects的其他問題,或提出您自己的問題。