`
sunxg
  • 浏览: 14255 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

A way of understanding the 'new' operator

 
阅读更多
			function myNew(constructor) {
				var obj = {
						__proto__: constructor.prototype
					};
				constructor.apply(obj, Array.prototype.slice.call(arguments, 1));
				return obj;
			}

			function MyTest(i) {
				this.count = i;
			}

			var obj1 = myNew(MyTest, 0);
			console.log(obj1.count);
			console.log(obj1 instanceof MyTest);
			console.log(obj1 instanceof Object);

			function OtherTest(i, name, value) {
				this.count = i;
				this.name = name;
				this.value = value;
			}

			var obj2 = myNew(OtherTest, 0, 'myName', 'myValue');
			console.log(obj2);
			console.log(obj2 instanceof MyTest);
			console.log(obj2 instanceof OtherTest);
分享到:
评论

相关推荐

    微软内部资料-SQL性能优化3

    This lesson outlines some of the common causes that contribute to the perception of a slow server. What You Will Learn After completing this lesson, you will be able to:  Describe locking ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    - **Creating a Visual Studio 2008 Project**: This section guides you through the process of setting up a new project in Visual Studio 2008, which is a powerful integrated development environment (IDE)...

    Java Crash Course [2016]

    book will guide you through the process, allowing you to expand your skills in Java more quickly than usual, making sense of ideas, understanding new concepts, and getting a better grasp of the ...

    JavaScript in 10 Minutes

    In summary, "JavaScript in 10 Minutes" is a comprehensive resource for intermediate to advanced JavaScript developers looking to deepen their understanding of the language's core features and advanced...

    sympy-docs-pdf-1.0.pdf

    Understanding the internal architecture of SymPy is beneficial for advanced users and contributors. Key components include the core, modules, and utilities. ##### 4.4 Contributing Contributing to ...

    Loving Lisp

    - **Extensibility**: Lisp’s macro system allows for the creation of new language constructs, making it highly extensible and customizable. #### Getting Started with CLISP To start programming in ...

    Tricks of the Windows video Game Programming---part1

    32 AM Page vi vi TRICKS OF THE WINDOWS GAME PROGRAMMING GURUS Programming the Microsoft Way: Hungarian Notation........................55 Variable Naming ....................................

Global site tag (gtag.js) - Google Analytics