Why this function in object method isn't work

I have two arrays which I want to match them together as below:


var a = [1, 2, 3, 4];
var b = [5, 6, 7, 8];

This method works:


for ( var i = 0; i < 4; i += 1 ) {
	(function(z) {
		var d = x(a[z], b[z]);
	})(i);
}

function x(j, k) {
	console.log(j, k);
};

But I also want this method to work as well:


for ( var i = 0; i < 4; i += 1 ) {
	(function(z) {
		var d = x.y(a[z], b[z]);
	})(i);
}

var x = {
	y: function(j, k) {
		console.log(j, k);
	}
};

Upon testing it throws error.
I woulds use the second method because most of my JavaScript code adopting the pattern.

The second example fails because “x” isn’t defined until after the for loop has run. Move “x” above the for loop, and it should be fine.

The only reason the first example works is because function declarations are special. JavaScript makes a point to look for and process those before it runs any regular code. The term “hoisting” is often used to describe this behavior. Function declarations behave as if they were hoisted to the top of your code.

Thank you for the info and explanation.