`

JavaScript Core chapter 10

    博客分类:
  • ajax
 
阅读更多

一些基础概念:

#1 JavaScript 对象其实就是属性的集合。属性由键值对组成。通过[属性名]这种形式则总是可以保证正确性。

#2 函数: 函数本身也是对象。

function func(id){};

相当于:

var func = function(id){};

#3 this 表示当前上下文,即调用者的引用。

#4 词法作用域 即是在定义的时候就确定下来了。

#5 scope 在执行一个函数时,函数的参数和其局部变量会作为调用对象的属性进行存储

#6 call & apply

#7  由于构造函数定义了一个对象的类,所以属性constructor在有助于确定给定对象的类型.如,可以使用如下代码来确定一个未知对象的类型:if ((typeof o == "object") && (o.constructor == Date)),也可以用instanceof运算符:if ((typeof o == "object") && (o instanceof Date))

#8 对象的引用机制:对象不会被复制,只能通过引用来传递。

var recordDefParams = [];

recordDefParams[0] = {name:'id',type:'date'};
recordDefParams[1] = {name:'version'};
recordDefParams[recordDefParams.length] = {name:'deleted'};
function func(recordDefParams){
    for (var i = 0, nLen = recordDefParams.length; i < nLen; i++) {
        var obj = recordDefParams[i];
        if (obj.type == 'date' && !obj.dateFormat) {
            obj.dateFormat = 'xxx-xx-x';
        }
    }
    return recordDefParams;
}
console.dir(recordDefParams);
console.log('===========================');
console.dir(func(recordDefParams));
 

#9

 

 

function addr(street, xno){
    console.log(this);
    this.street = street || 'beijinglu';
    this.xno = xno || '1 hao';
    this.toString = function(){
        return 'Street:' + this.street + ' xno:' + this.xno;
    };
};
var core1 = new addr();
console.log(core1.toString());
var core2 = new addr('北京路','一号');
console.log(core2.toString());
 

 

//动态构建新的匿名对象
function point(x,y){
    this.x = x;
    this.y = y;
    return {'x':x,'y':y};
}
var a = point(3,4);
console.dir(a);
for(var key in a ){
   console.log(key);
   console.log(a[key]);
}
 

 

After read the blog of "JavaScript Core chapter 10 .writer the coding below for testing.

<html>
	<head>
		<title>
			 Js core
		</title>
		<script type="text/javascript">
			//原型链
			var base = {
				name : 'Mark',
				getName : function(){
					return this.name;
				}
			};
			var ext1 ={
				id :'0',
				__proto__ :base
			};
			var ext2 ={
				id : '9',
				__proto__ : base
			};
			//console.log(ext1.getName());
			
			//构造器
			function Task(id){
				this.id = id;
			};
			Task.prototype.status = 'defalt_begin';
			Task.prototype.execute = function(args){
				return 'execute task_[' + this.id + ']' + this.status + ':' + args;
			};
			var task1 = new Task(1);
			var task2 = new Task(2);
			task1.status = 'activing';
			task2.status = 'end...';
			//console.log(task1.execute('task_1'));
			//console.log(task2.execute('task_2'));
			
			//this understander
			var global = this;
			var tom = {
				name : 'tom',
				id : '1',
				getInfo: function(){
					console.log(this.name + ':' + this.id);
				}
			}
			var jetty = {
				name : 'jetty',
				getInfo : tom.getInfo 
				//等同于
				//getInfo:function (){ console.log(this.name + ':' + this.id);} 在跑构造的时候就创建好了。
			}
			
			tom.getInfo();
			jetty.getInfo();
			global.getInfo = tom.getInfo;
			global.getInfo();
		</script>
	</head>
	<body>
		 js core chapter 10 & 11.
	</body>
</html>

   all : 给全局对象动态的添加一个属性。

分享到:
评论

相关推荐

    JavaScript权威指南(第6版).JavaScript:The.Definitive.Guide

    Since 1996, JavaScript: The Definitive Guide has been the bible for JavaScript programmers—a programmer's guide and comprehensive reference to the core language and to the client-side JavaScript APIs...

    JavaScript权威指南(第6版)

    Since 1996, [removed] The Definitive Guide has been the bible for JavaScript programmers—a programmer's guide and comprehensive reference to the core language and to the client-side JavaScript APIs ...

    Beginning.JavaScript.5th.Edition

    Title: Beginning JavaScript, 5th Edition Author: Jeremy McPeak Length: 768 pages ...Appendix B: Javascript Core Reference Appendix C: W3C Dom Reference Appendix D: Latin-1 Character Set

    JavaScript Functional Programming for JavaScript Developers (PDF, EPUB, MOBI)

    Chapter 10: Messaging Patterns Chapter 11: Microservices Chapter 12: Patterns for Testing Chapter 13: Advanced Patterns Chapter 14: ECMAScript-2015/2016 Solutions Today Module 3: Functional ...

    Data Wrangling with JavaScript

    Data Wrangling with JavaScript teaches readers core data munging techniques in JavaScript, along with many libraries and tools that will make their data tasks even easier. Table of Contents Chapter ...

    JavaScript 圣经第5版-Javascript编程宝典--黄金版 .rar

    Chapter 10: Strings, Math, and Dates. Chapter 11: Scripting Frames and Multiple Windows. Chapter 12: Images and Dynamic HTML. Part III: Document Objects Reference. Chapter 13: JavaScript ...

    Functional.Programming.in.JavaScript.1784398225

    The book first explores the core concepts of functional programming common to all functional languages, with examples of their use in JavaScript. It's followed by a comprehensive roundup of functional...

    高清Building Web Applications with Visual Studio 2017及源码

    Chapter 2: Building the Data Access Layer with Entity Framework Core Chapter 3: Building the RESTful Service with ASP.NET Core MVC Services Chapter 4: Introducing ASP.NET Core MVC Web Applications ...

    [ASP.NET.3.5高级程序设计(第2版)].Pro.ASP.NET.3.5.in.C#.2008.2th.edtion.pdf

    CHAPTER 10 Rich Data Controls 385 CHAPTER 11 Caching and Asynchronous Pages 451 CHAPTER 12 Files and Streams 497 CHAPTER 13 LINQ 531 CHAPTER 14 XML 587 PART 3 Building ASP.NET Websites CHAPTER ...

    Developing.a.Redux.Edge.B01KL5RJHU

    This book is for anyone wanting to learn about Redux, a predictable state container for JavaScript apps. It is aimed at intermediate developers who have a good understanding ...Chapter 10. Going offline

    毕业设计Chapter02

    4. **编程语言与框架**:"Accp6.0"可能是指某种编程语言或框架,如Java的Spring Boot或.NET的ASP.NET Core。学生需要熟练掌握相关语言特性和框架的使用方法。 5. **前端开发**:HTML、CSS和JavaScript是基础,可能...

    Learning.Node.Moving.to.the.Server-Side.2nd.Edition.14919

    Early Realease Take your web development skills from browser to server with ...Chapter 10. Full-stack Node Development Chapter 11. Node in Development and Production Chapter 12. Node in New Environments

    Practical Ext JS 4.pdf

    Chapter 1: Core JavaScript and JavaScript Frameworks Chapter 2: Overview of Ext JS 4 Chapter 3: Understanding the Ext JS 4 API Chapter 4: Controls and Layout Chapter 5: Working with Data Chapter 6: ...

Global site tag (gtag.js) - Google Analytics