You do not know the type of transformation and the prototype js chain

Last night received a surface electric ants gold dress. Which has a question, I was very impressed, after the end, I went to check the information, wrote an article to summarize the poor.

problem

var a = {}; a.__proto__ === ?
var a = 1; a.__proto__ === ?

Was ambiguous, I know they are the top Object.prototypedirectly answer this option, because it was not thinking of all the objects you are, the prototype chain top the object is not Object.prototypeit? And why have digital prototype chain? Mind emerges out Functin, Array various data types, but is still very vague, After answering the interviewer did not continue to ask (probably think I do not know it), but the problem has always been my heart in a knot, check some information after, and its finishing.

As we all know, JavaScript data types are divided into two types, one is the basic data type, the other is a reference data types.

What is the basis of the type of ( primitive )?

[A primitive (primitive value, primitive type Data) Data that IS IS AN Not Object and has NO Methods .] No data attributes and methods.

The type of conversion

First we look at the second question, I started to explain, from the second question var a = 1; a.__proto__=== ?to the browser running under can know, a.__proto__=== Number.prototypeand then we run so a.__proto__.__proto__=== Object.prototypethe result turned out to be true.

In the front we said, js two types, why a top digital prototype chain (a) is the prototype of Object? How the two are linked? primitive is not without attributes and methods do? Where to prototype chain?

It was such, js language is weak, if he finds time type mismatch, why would he? He would type conversion (Auto Convert) ah.

Therefore, the above problem becomes deformed look at var a = 1; new Number(a).__proto__=== ?this problem becomes very clear, and a Number method, construct an instance of Number, then the prototype chain is certainly the prototype Number of ah (ie Number.prototype). Further, examples of a method constructed of Number, there must be the prototype chain. Now that is a case in point is an object, and then further on, must be Object.prtotype.

Look again at the chestnuts.

var a = "abc";
console.info(a.length);

var b = 1;
console.info(b.toString());

This is what we usually have been used to understand the above questions, your heart must know the reason why a, b are primitive , but why there are other attributes. But also because the conversion type, equivalent to

var a = "abc";
console.info(new String(a).length);
var b = 1;
console.info(new Number(b).toString());

summary

Summing up the above, therefore, js basic types and reference types do not matter! It does not matter! A relationship so that they are! Type of conversion. (Js because their own reasons, to force them had relations). And no base-type method, no attribute.

So I think the phrase, js things are objects, really a little deceptive.

Prototype chain

But then I thought, is not some function, array, date stuff, right, that what is, what belongs to. This time I bring them a clear rationale. There are those constructors forced relationship occurred, such as Number, String, Boolean, Date?

That is all I sort of relationship as well as a reference to the type of the prototype chain.

Tip (Some students may not understand the misunderstanding):

(I Date, Number, Boolean, String classified to Function. Objec.prototype rather just said is what the prototype chain, that is what you have to figure out methods and new methods (), by the new method becomes the object Object The following are also some proof)

summary

Object just as in js in a complex type that contains a number of subsets, but still belongs to the type of Object and basic parallel relationship.

extend

With the above understanding, but also create their own method a prototype chains determine the type. (Because it is said by Object.prototype.toString.call () such implementations a bit ugly and strange way, people will not understand what you write is what it is.)

var a = null;	
var b = 1;
var c = '1';
var d = undefined;
var g = true;
var e = function (){};
var f = [];
var h = new Date();
var i = {}

function type(a) {
	if(a === null) {
		return 'null';
	}
	if(typeof a === 'number') {
		return 'number';
	}
	if(typeof a === 'string') {
		return 'string'
	}
	if(typeof a === 'undefined') {
		return 'undefined';
	}
	if(typeof a === 'boolean') {
		return 'boolean';
	}
	if(a instanceof Array) {
		return 'array';
	}
	if(a instanceof Date) {
		return 'date';
	}
	if(a instanceof Function) {
		return 'function';
	}
	if(a instanceof Object) {
		return 'object';
	}
}

console.log(type(a)); // null 
console.log(type(b)); // number
console.log(type(c)); // string
console.log(type(d)); // undefined
console.log(type(f)); // array
console.log(type(e)); // function
console.log(type(g)); // boolean
console.log(type(h)); // date
console.log(type(i)); // object

to sum up

By the above described relation wanted nothing basis and reference types, and each type of prototype chain. I said a little chaotic, if there is something wrong please ask, I promptly corrected, so as to avoid misleading. Leaving a question, if you understand the above said, then you necessarily know the answer.

var a = 1;
a.a = 1;
console.log(a.a);

Guess you like

Origin www.cnblogs.com/qianduanwriter/p/11800893.html