使用内置的 Object() 构造函数,我们可以动态创建通用的空对象。事实上,如果你还记得第一章的开头,这正是我们通过创建 cody 对象所做的事情。让我们重新创建 cody 对象。
示例:sample69.html
<!DOCTYPE html><html lang="en"><body><script> var cody = new Object(); // Create an empty object with no properties. for (key in cody) { // Confirm that cody is an empty generic object. if (cody.hasOwnProperty(key)) { console.log(key); // Should not see any logs, because cody itself has no properties. } } </script></body></html>
在这里,我们所做的就是使用 Object()
构造函数来创建一个名为 cody 的通用对象。您可以将 Object()
构造函数视为一个千篇一律的工具,用于创建没有预定义属性或方法的空对象(当然,从原型链继承的对象除外)。
如果不是很明显,Object()
构造函数本身就是一个对象。也就是说,构造函数基于从 Function
构造函数创建的对象。这可能会令人困惑。请记住,与 Array
构造函数一样,Object
构造函数只是吐出空白对象。是的,您可以创建您喜欢的所有空对象。但是,创建像 cody 这样的空对象与使用预定义属性创建自己的构造函数有很大不同。确保您了解 cody 只是一个基于 Object()
构造函数的空对象。要真正利用 JavaScript 的力量,您不仅需要学习如何从 Object()
创建空对象容器,还需要学习如何构建您自己的对象“类”(Person()
),例如Object()
构造函数本身。
Object()
参数
Object()
构造函数采用一个可选参数。该参数是您想要创建的值。如果您未提供任何参数,则将假定 null
或 undefined
构造函数采用一个可选参数。该参数是您想要创建的值。如果您未提供任何参数,则将假定 null
或 undefined
值。
示例:sample70.html
<!DOCTYPE html><html lang="en"><body><script> // Create an empty object with no properties. var cody1 = new Object(); var cody2 = new Object(undefined); var cody3 = new Object(null); console.log(typeof cody1, typeof cody2, typeof cody3); // Logs 'object object object'. </script></body></html>
如果将 null
或 undefined
之外的值传递给 Object
构造函数,则传递的值将被创建为对象。因此理论上,我们可以使用 null
或 undefined
之外的值传递给 Object
构造函数,则传递的值将被创建为对象。因此理论上,我们可以使用 Object()
构造函数来创建任何其他具有构造函数的本机对象。在下一个示例中,我就是这么做的。
示例:sample71.html
<!DOCTYPE html><html lang="en"><body><script> /* Use the Object() constructor to create string, number, array, function, Boolean, and regex objects. */ // The following logs confirm object creation. console.log(new Object('foo')); console.log(new Object(1)); console.log(new Object([])); console.log(new Object(function () { })); console.log(new Object(true)); console.log(new Object(/\bt[a-z]+\b/)); /* Creating string, number, array, function, Boolean, and regex object instances via the Object() constructor is really never done. I am just demonstrating that it can be done. */ </script></body></html>
Object()
属性和方法
Object()
对象具有以下属性(不包括继承的属性和方法):
属性(Object.prototype;
):
原型
Object()
实例属性和方法
Object()
对象实例具有以下属性和方法(不包括继承的属性和方法):
实例属性 (var myObject = {};
myObject.constructor;
):
构造函数
实例方法 (var myObject = {};
myObject.toString();
):
hasOwnProperty()
isPrototypeOf()
propertyIsEnumerable()
toLocaleString()
toString()
valueOf()
原型链以Object.prototype
结尾,因此Object()
结尾,因此
Object()
使用“对象文字”创建
对象
var cody = {};
创建“对象文字”需要使用大括号实例化带有或不带有属性的对象(
示例:sample72.html
<!DOCTYPE html><html lang="en"><body><script> var cody = new Object(); cody.living = true; cody.age = 33; cody.gender = 'male'; cody.getGender = function () { return cody.gender; }; console.log(cody); // Logs cody object and properties. </script></body></html>
cody
对象及其属性需要五个语句。使用对象文字表示法,我们可以在一条语句中表达相同的 cody
请注意,代码中创建 对象。示例:sample73.html
<!DOCTYPE html><html lang="en"><body><script> var cody = { living: true, age: 23, gender: 'male', getGender: function () { return cody.gender; } }; // Notice the last property has no comma after it. console.log(cody); // Logs the cody object and its properties. </script> </body>
:
和 ,
使用文字表示法使我们能够使用更少的代码创建对象(包括定义的属性)并直观地封装相关数据。请注意在单个语句中使用 运算符。由于其简洁性和可读性,这实际上是在 JavaScript 中创建对象的首选语法。您应该知道属性名称也可以指定为字符串:
示例:sample74.html
<!DOCTYPE html><html lang="en"><body><script> var cody = { 'living': true, 'age': 23, 'gender': 'male', 'getGender': function () { return cody.gender; } }; console.log(cody); // Logs the cody object and its properties. </script> </body>没有必要将属性指定为字符串,除非属性名称:
-
class
是保留关键字之一( )。 - 包含空格或特殊字符(数字、字母、美元符号 ($) 或下划线 (_) 字符以外的任何字符)。
- 以数字开头。
小心!对象的最后一个属性不应有尾随逗号。这会在某些 JavaScript 环境中导致错误。
Object.prototype
所有对象继承自
Object()
构造函数比较特殊,因为它的prototype
JavaScript中的
prototype
属性是原型链中的最后一站。🎜
在以下示例中,我使用 foo
属性扩充 Object.prototype
,然后创建一个字符串并尝试访问 foo
属性,就好像它是字符串实例的属性一样。由于 myString
实例没有 foo
属性,因此原型链启动并在 String.prototype
中查找值。它不在那里,所以下一个要查找的位置是 Object.prototype
,这是 JavaScript 查找对象值的最终位置。找到了 foo
值,因为我添加了它,因此它返回 foo
的值。
示例:sample75.html
<!DOCTYPE html><html lang="en"><body><script> Object.prototype.foo = 'foo'; var myString = 'bar'; // Logs 'foo', being found at Object.prototype.foo via the prototype chain. console.log(myString.foo); </script> </body>
结论
小心!添加到 Object.prototype
的任何内容都将显示在 for in
循环和原型链中。因此,据说禁止更改 Object.prototype
。
以上是重新表达的标题为:The Concept of Object()的详细内容。更多信息请关注PHP中文网其他相关文章!

Yes,youcanuseWordPresstobuildamembershipsite.Here'show:1)UsepluginslikeMemberPress,PaidMemberSubscriptions,orWooCommerceforusermanagement,contentaccesscontrol,andpaymenthandling.2)Ensurecontentprotectionwithupdatedpluginsandadditionalsecuritymeasures

你不需要编程知识就能使用WordPress,但掌握编程可以提升体验。1)使用CSS和HTML可以调整主题样式。2)PHP知识能编辑主题文件,添加功能。3)自定义插件和元标签可优化SEO。4)注意备份和使用子主题以防更新问题。

TosecureaWordPresssite,followthesesteps:1)RegularlyupdateWordPresscore,themes,andpluginstopatchvulnerabilities.2)Usestrong,uniquepasswordsandenabletwo-factorauthentication.3)OptformanagedWordPresshostingorsecuresharedhostingwithawebapplicationfirewal

WordPressExcelSoverotherWeberteBuilderSduetoItsflexible,可伸缩性,andopen-sourcenature.1)它'saversatilecmswithExtEnsextEnsiveCustomizedOptionsVIATHEMESANDPLUGINS.2)它的alllearbutoffersbutoffersbutoffersbutoffersbutofferspopelyContrololonCemastered.3)

2025年网站开发的七个必备WordPress插件 在2025年建立顶级WordPress网站需要速度,响应能力和可扩展性。 实现这种有效的实现通常取决于战略插件的选择。 这篇文章Highlig

WordPresscanbeusedforvariouspurposesbeyondblogging.1)E-commerce:WithWooCommerce,itcanbecomeafullonlinestore.2)Membershipsites:PluginslikeMemberPressenableexclusivecontentareas.3)Portfoliosites:ThemeslikeAstraallowstunninglayouts.Ensuretomanageplugins

是的,wordpressisisexcellentforcortingaportfoliowebsite.1)itoffersnumeroversnumeroverportfolio-spificthemeslike'astra'astra'astra'astra'astra'astra'astra'astra'astra'elementor'Enelementor'enableIntiviveSiveSign,Thoughtemanycanslowthesite.3)

WordPressisadvantageousovercodingawebsitefromscratchdueto:1)easeofuseandfasterdevelopment,2)flexibilityandscalability,3)strongcommunitysupport,4)built-inSEOandmarketingtools,5)cost-effectiveness,and6)regularsecurityupdates.Thesefeaturesallowforquicke


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3汉化版
中文版,非常好用

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。