列表

详情


FED14. Object.freeze

描述

请补全JavaScript代码,要求实现Object.freeze函数的功能且该新函数命名为"_objectFreeze"

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1683ms, 内存消耗: 77800KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _objectFreeze = object => {\n                // 补全代码\n                for(prop in object) {\n                    const type = Object.prototype.toString.call(object[prop])\n                    if(type === '[Object object]' || type === '[Object array]') {\n                        _objectFreeze(object[prop])\n                    }else {\n                        Object.defineProperty(object,prop,{\n                            writable:false\n                        })\n                    }\n                }\n                Object.seal(object)\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77832KB, 提交时间: 2021-12-17

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _objectFreeze = object => {\n                // 补全代码\n                for(let item in object){\n                        Object.defineProperty(object,item,{\n                            writable: false,\n                            configurable: false,\n                        }) \n                    }\n               Object.preventExtensions(object)\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1743ms, 内存消耗: 77772KB, 提交时间: 2022-01-24

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _objectFreeze = object => {\n                // 补全代码\n                for(let key in object) {\n                    if (object.hasOwnProperty(key)) {\n                        Object.defineProperty(object, key, {\n                            writable: false\n                        })\n                    }\n                }\n                Object.seal(object)\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1744ms, 内存消耗: 77772KB, 提交时间: 2022-01-24

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _objectFreeze = object => {\n                // 补全代码\n                if(typeof object != 'object'){\n                    return object;\n                }\n                for(let key in object){\n                    if(typeof object[key] == 'object' || Array.isArray(object[key])){\n                        _objectFreeze(object[key])\n                    }else{\n                        // 冻结对象\n                        Object.defineProperty(object,key,{\n                            writable:false,\n                            configurable:false,\n                            enumerable:false,\n                        })\n                    }\n                }\n                // 禁止扩展\n                Object.preventExtensions(object)\n                return object;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1749ms, 内存消耗: 77772KB, 提交时间: 2021-12-14

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n   \nconst _objectFreeze = (object) => {\n  Object.keys(object).forEach((key) => {\n    Object.defineProperty(object, key, {\n      writable: false,\n    });\n  });\n  Object.preventExtensions(object);\n  return object;\n};\n        </script>\n    </body>\n</html>","libs":[]}

上一题